[英]How can i increment the rows in excel using apache poi
我正在編寫一個程序,以使用java和apache poi來記錄Excel表格來跟蹤員工的座位。 該程序將提示用戶輸入辦公桌編號,員工姓名和坐在該辦公桌上的員工人數。 我的問題是,該程序僅寫入用戶輸入的最后信息,從而刪除了先前在該行中輸入的信息。 如何保留Excel工作表第一行中輸入的內容? 我將其設置在一個循環中,在該循環中,將一直提示用戶輸入座位信息,直到用戶輸入EXIT。 我希望每次輸入新信息時它都會向下遞增。
這是我到目前為止的內容:
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.*;
public class EmployeeSeatingRep {
public static void main(String[] args){
String deskNumber = "";
String employeeName = "";
int empsAtDesk = 0;
boolean keepRunning = true;
//Blank workbook
HSSFWorkbook workbook = new HSSFWorkbook();
//Blank sheet
HSSFSheet sheet = workbook.createSheet("Seating Details");
//create heading
Row rowHeading = sheet.createRow(0);
rowHeading.createCell(0).setCellValue("Desk:");
rowHeading.createCell(1).setCellValue("Employee(s)Name:");
rowHeading.createCell(2).setCellValue("Number At Desk:");
//Font size and style loop for my headers
for(int i = 0; i < 3; i++)
{
CellStyle stylerowHeading = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short) 11);
stylerowHeading.setFont(font);
stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowHeading.getCell(i).setCellStyle(stylerowHeading);
}
while(keepRunning){
Scanner scnr = new Scanner(System.in);
System.out.print("Enter desk number: ");
deskNumber = scnr.nextLine();
if(deskNumber.equals("EXIT"))
{
System.out.print("You have ended the program");
System.exit(0);
}
else
{
System.out.print("Enter employee name: ");
employeeName = scnr.nextLine();
System.out.print("Enter amount of employees at desk: ");
empsAtDesk = scnr.nextInt();
//This data needs to be written (Object[])
Map <String, Object[]> data = new TreeMap<String, Object[]>();
data.put("2", new Object[] {deskNumber, employeeName, empsAtDesk});
//Iterate over data and write to sheet
Set<String> keyset = data.keySet();
int rownum = 1;
for(String Key : keyset)
{
Row row = sheet.createRow(rownum++);
Object [] objArr = data.get(Key);
int cellnum = 0;
for(Object obj : objArr)
{
Cell cell = row.createCell(cellnum++);
if(obj instanceof String)
{
cell.setCellValue((String)obj);
}
else if(obj instanceof Integer)
{
cell.setCellValue((Integer)obj);
}
}
if(!(deskNumber.equals("EXIT")))
{
rownum++;
}
//Auto size my columns that will be filled out with user input info.
for (int i = 0; i < 3; i++)
{
sheet.autoSizeColumn(i);
}
}
try{
//save to excel file
FileOutputStream out = new FileOutputStream(new File("Employee Seating Report.xls"));
workbook.write(out);
out.flush();
out.close();
System.out.println("Excel Written Succesfully...");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}//pub static void end brace
}//pub class end brace
您正在描述的問題是由以下原因引起的:
int rownum = 1; <--
for(String Key : keyset)
{
Row row = sheet.createRow(rownum++);
您已經在主循環中聲明了rownum
,因此,它將始終為1。
如果不使用循環來構建,填充和寫入data
映射,而是使用循環來構建行對象List
,然后在用戶完成操作后將其寫入文件,則可以大大簡化操作。
用戶輸入的每個信息都應添加到excel中的新行中,因此您需要在for循環外創建一行,並且僅在for循環中為輸入的數據創建單元格。 像這樣:
Scanner scnr = new Scanner(System.in);
System.out.print("Enter desk number: ");
deskNumber = scnr.nextLine();
if(deskNumber.equals("EXIT"))
{
System.out.print("You have ended the program");
System.exit(0);
}
else
{
System.out.print("Enter employee name: ");
employeeName = scnr.nextLine();
System.out.print("Enter amount of employees at desk: ");
empsAtDesk = scnr.nextInt(); scnr.next();
Row row = sheet.createRow(rownum++);
Object [] objArr = new Object[] {deskNumber, employeeName, empsAtDesk};
int cellnum = 0;
for(Object obj : objArr)
{
Cell cell = row.createCell(cellnum++);
if(obj instanceof String)
{
cell.setCellValue((String)obj);
}
else if(obj instanceof Integer)
{
cell.setCellValue((Integer)obj);
}
}
}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.