Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ApachePOI将数据写入java中的excel文件_Java_Apache Poi - Fatal编程技术网

使用ApachePOI将数据写入java中的excel文件

使用ApachePOI将数据写入java中的excel文件,java,apache-poi,Java,Apache Poi,在使用ApachePOI将一些数据写入Excel工作表时,我遇到了一个问题。 我想多次调用一个函数,将数据写入我创建的Excel工作表中 是否有任何方法可以在每次函数结束时将指针移动到下一行 我的代码如下 public static void excelLog(String filename , String message) { String dest="D:\\testexcel.xls"; HSSFWorkbook myWorkBook = new HSSFWor

在使用ApachePOI将一些数据写入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();
    }           
 }
publicstaticvoidexcellog(字符串文件名、字符串消息)
{  
String dest=“D:\\testexcel.xls”;
HSSFWorkbook myWorkBook=新建HSSFWorkbook();
HSSFSheet mySheet=myWorkBook.createSheet();
HSSFRow myRow=null;
HSSFCell-myCell=null;
字符串excelData[][]=新字符串[1][2];
excelData[0][0]=文件名;
excelData[0][1]=消息;
myRow=mySheet.createRow(rowNum);
对于(int-cellNum=0;cellNum<2;cellNum++){
myCell=myRow.createCell((短)cellNum);
myCell.setCellValue(excelData[rowNum][cellNum]);
}
rowNum++;
试一试{
FileOutputStream out=新的FileOutputStream(dest);
我的工作手册。写出来;
out.close();
}捕获(例外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);
}
import java.io.FileOutputStream;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
公共类{
私有静态字符串dest=“D:\\testexcel.xls”;
私有静态HSSFWorkbook myWorkBook=新建HSSFWorkbook();
私有静态HSSFSheet mySheet=my工作簿.createSheet();
公共静态void excelLog(字符串文件名、字符串消息、int rowNum){
HSSFRow myRow=null;
HSSFCell-myCell=null;
字符串excelData[][]=新字符串[1][2];
excelData[0][0]=文件名;
excelData[0][1]=消息;
myRow=mySheet.createRow(rowNum);
对于(int-cellNum=0;cellNum<2;cellNum++){
myCell=myRow.createCell(cellNum);
myCell.setCellValue(excelData[0][cellNum]);
}
试一试{
FileOutputStream out=新的FileOutputStream(dest);
我的工作手册。写出来;
out.close();
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
对于(int i=0;i<10;i++)
Excel日志(“文件名”+i,“消息”+i,i);
}

}

这是经过修改的代码,因此它支持动态列大小。其思想是创建一行,如果该行已经存在,则重用该行

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();
    }
}
import java.io.FileOutputStream;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
公共类{
私有静态字符串dest=“D:\\testexcel.xls”;
私有静态HSSFWorkbook myWorkBook=新建HSSFWorkbook();
私有静态HSSFSheet mySheet=my工作簿.createSheet();
私有静态void excelLog(int行、int列、字符串值){
HSSFRow myRow=mySheet.getRow(row);
if(myRow==null)
myRow=mySheet.createRow(行);
HSSFCell myCell=myRow.createCell(col);
myCell.setCellValue(值);
}
公共静态void main(字符串[]args){
int numCol=10;//假设为10列
对于(int i=0;i<10;i++){
对于(int j=0;j

}

你能告诉我一件事吗?我想知道的是,我不想在excel表格中插入两个冒号,所以我使用了二维数组。如果我必须在表格中填写5列,那么使用数组将太复杂了,对吗???那么这里的替代品是什么呢?还有一件事我想知道,这里我只想在excel表中插入两列,所以我使用了二维数组。如果我必须在工作表中填写5列,那么使用数组将过于复杂,对吗???那么这里的替代品是什么呢??