0

我在使用 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();
    }           
 }

请帮助我。谢谢 :)

4

2 回答 2

4

这是修改后的代码,因此它支持动态列大小。这个想法是创建一次行并在该行已经存在时重用它。

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();
    }
}

}

于 2012-06-27T10:30:37.140 回答
3

您的代码的问题在于每次调用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);
}

}

于 2012-06-27T09:03:07.937 回答