我想在Excel文件中添加文字。我可以添加一次。但是,当我第二次尝试时,只更新最后一个值。我尝试了其他一些东西,但我没有得到答案。
while (true) {
try {
clientSocket = serverSocket.accept(); //accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //get the client message
message = bufferedReader.readLine();
System.out.println("Java Server1 " + message);
writeInExcel(message);
}
public static void writeInExcel(String stock_list) throws WriteException, IOException, BiffException
{
Workbook existingWorkbook = Workbook.getWorkbook(new File("try.xls"));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File("output.xls"), existingWorkbook);
WritableSheet sheetToEdit = workbookCopy.getSheet(0);
Label l = new Label(0, i, String.valueOf( i));
sheetToEdit.addCell(l);
workbookCopy.write();
workbookCopy.close();
existingWorkbook.close();
}
在这里,只要我在输入流上获得某些内容,我就需要将它添加到excel文件中。 如果有人可以帮助我或者可以解释一下代码,那就太好了。在可写单元格之后,我无法完全理解代码
答案 0 :(得分:1)
由于在您的代码中,您已将以下内容放在for循环中,请将其置于循环外
workbookCopy.write();
workbookCopy.close();
existingWorkbook.close();
试试这个
Workbook existingWorkbook = Workbook.getWorkbook(new File("try.xls"));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File("output.xls"), existingWorkbook);
WritableSheet sheetToEdit = workbookCopy.getSheet(0);
for(int i=0;i<5;i++)
{
Label l = new Label(0, i, String.valueOf( i));
sheetToEdit.addCell(l);
}
workbookCopy.write();
workbookCopy.close();
existingWorkbook.close();
<强> output.xls 强> 当我把limit = 5