[英]write data to excel file in java using apache POI
我在使用Apache POI將一些數據寫入Excel工作表時遇到問題。 我想多次調用一個函數,該函數會將數據寫入我創建的Excel工作表中。
有什么方法可以在函數結尾每次將指針移到下一行???
我的代碼在下面給出...
public static void excelLog(String filename , String message)
{
String dest="D:\\testexcel.xls";
HSSFWorkbook myWorkBook = new HSSFWorkbook();
HSSFSheet mySheet = myWorkBook.createSheet();
HSSFRow myRow = null;
HSSFCell myCell = null;
String excelData [][] = new String [1][2];
excelData[0][0]=filename;
excelData[0][1]=message;
myRow = mySheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 2 ; cellNum++){
myCell = myRow.createCell((short) cellNum);
myCell.setCellValue(excelData[rowNum][cellNum]);
}
rowNum++;
try{
FileOutputStream out = new FileOutputStream(dest);
myWorkBook.write(out);
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
請幫助我。 謝謝 :)
這是經過修改的代碼,因此它支持動態列大小。 想法是創建一次行,如果該行已經存在,則重用它。
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class PoiSample {
private static String dest = "D:\\testexcel.xls";
private static HSSFWorkbook myWorkBook = new HSSFWorkbook();
private static HSSFSheet mySheet = myWorkBook.createSheet();
private static void excelLog(int row, int col, String value) {
HSSFRow myRow = mySheet.getRow(row);
if (myRow == null)
myRow = mySheet.createRow(row);
HSSFCell myCell = myRow.createCell(col);
myCell.setCellValue(value);
}
public static void main(String[] args) {
int numCol = 10; // assume 10 cols
for (int i = 0; i < 10; i++) {
for (int j = 0; j < numCol; j++) {
excelLog(i, j, "Row : " + i + ", Cell : " + j);
}
}
try {
FileOutputStream out = new FileOutputStream(dest);
myWorkBook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
代碼的問題在於每次調用excelLog
方法時,它將生成新的excel文件。 我如下更改了您的代碼。
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class PoiSample {
private static String dest = "D:\\testexcel.xls";
private static HSSFWorkbook myWorkBook = new HSSFWorkbook();
private static HSSFSheet mySheet = myWorkBook.createSheet();
public static void excelLog(String filename, String message, int rowNum) {
HSSFRow myRow = null;
HSSFCell myCell = null;
String excelData[][] = new String[1][2];
excelData[0][0] = filename;
excelData[0][1] = message;
myRow = mySheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 2; cellNum++) {
myCell = myRow.createCell(cellNum);
myCell.setCellValue(excelData[0][cellNum]);
}
try {
FileOutputStream out = new FileOutputStream(dest);
myWorkBook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++)
excelLog("filename " + i, "message " + i, i);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.