2012-06-27 35 views
0

我在使用Apache POI將一些數據寫入Excel工作表時遇到問題。 我想多次調用一個函數,它會將數據寫入我創建的Excel工作表中。使用Apache POI將數據寫入java中的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(); 
    }   
} 

請幫我在這。 謝謝:)

+0

一兩件事我想知道的是,這裏,我只想在Excel工作表,所以我使用2-d陣列插入兩列。如果我必須在表格中填充5列,那麼使用數組將太複雜了嗎?那麼這裏的替代品是什麼? – ASH

回答

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

}

+0

非常感謝你 – ASH

+0

你能告訴我一件事 我想知道在這裏,我想在excel表中插入兩個coloumns,所以我使用了二維數組。如果我必須在表格中填充5個色彩,那麼使用數組將會太複雜了嗎? 那麼這裏的替代品是什麼? – ASH

+0

您可以使用地圖而不是二維數組.. –

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

}