1

我是 Java POI 的新手,我正在尝试使用 Java POI 覆盖一个 excel 文件。让我说清楚,我不想每次构建代码时都打开一个新的 .xls 文件,但是我写的代码确实如此这样做的目的是,我将在 excel 上构建图表并从数据库中读取图表的值,然后使用 Java POI 将其写入 excel 文件。这是我的代码:

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet firstSheet = workbook.createSheet("oldu");
HSSFSheet secondSheet = workbook.createSheet("oldu2");

HSSFRow rowA = firstSheet.createRow(6);
HSSFCell cellA = rowA.createCell(3);
cellA.setCellValue(new HSSFRichTextString("100"));
cellA.setCellValue(100);

HSSFRow rowB = secondSheet.createRow(0);
HSSFCell cellB = rowB.createCell(0);
cellB.setCellValue(new HSSFRichTextString("200"));

FileOutputStream fos = null;
try {
    fos = new FileOutputStream(new File("CreateExcelDemo.xls"));
    workbook.write(fos);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (fos != null) {
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
4

6 回答 6

1

始终使用相同的文件名,当您运行程序时,它将覆盖该文件。

编辑 如果您想修改现有的 excel 文件,请查看此处并向下滚动到“读取或修改现有文件”部分。

于 2012-06-13T11:28:14.733 回答
0

问题是 Apache POI 不支持 Excel 文件格式的所有功能,包括图表。因此无法使用 POI 生成图表,并且在使用 POI 打开现有 Excel 文件并对其进行修改时,Excel 文件中的一些当前“对象”可能会丢失,因为 POI 无法处理它,并且在写入时,只写入生成的新信息和可以处理的现有信息。

Apache 认为这是 POI 的缺陷之一。

我们对现有的 Excel 文件做了类似的处理,在上面填充了新的数据,但是现有的 Excel 文件只包含格式样式,它们是使用 POI 保留的,但我认为图表很有问题。尝试使用 POI 填充数据来更新现有图表是不可能的。

于 2012-06-13T11:41:04.877 回答
0
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.*;
import java.util.List;

public class WriteExcel {
    public static void writeFile1(List dataList , String path , int columnCount,int forwardIndex)  throws InvalidFormatException, IOException {

        try {
            FileInputStream inputStream = new FileInputStream(new File(path));
            Workbook workbook = WorkbookFactory.create(inputStream);

            Sheet sheet = workbook.getSheetAt(0);
            int rowCount = 1;

            for ( int i = 0+forwardIndex ; i < dataList.size(); i++) {
                Row row = sheet.createRow(++rowCount);
              //  int columnCount = 0;

                Cell cell = row.createCell(columnCount);
                cell.setCellValue((String) dataList.get(i));
            }

            inputStream.close();

            FileOutputStream outputStream = new FileOutputStream(path);
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.*;
import java.util.List;

public class WriteExcel {
    public static void writeFile1(List dataList , String path , int columnCount,int forwardIndex)  throws InvalidFormatException, IOException {

        try {
            FileInputStream inputStream = new FileInputStream(new File(path));
            Workbook workbook = WorkbookFactory.create(inputStream);

            Sheet sheet = workbook.getSheetAt(0);
            int rowCount = 1;

            for ( int i = 0+forwardIndex ; i < dataList.size(); i++) {
                Row row = sheet.createRow(++rowCount);
              //  int columnCount = 0;

                Cell cell = row.createCell(columnCount);
                cell.setCellValue((String) dataList.get(i));
            }

            inputStream.close();

            FileOutputStream outputStream = new FileOutputStream(path);
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2018-03-19T06:58:22.550 回答
0
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class UserInput {

    public static Map getUserInput() throws InvalidFormatException, IOException {
        String userName = System.getProperty("user.name");
        String path = "";
        int indexofColumn = 10;
        Map inputFromExcel = new HashMap();
        InputStream inp = new FileInputStream(path);
        int ctr = 4;
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0); // Mention Sheet no. which you want to read
        Row row = null;
        Cell cell = null;
            try{
                row = sheet.getRow(ctr);
                cell = row.getCell(indexofColumn); //Mention column no. which you want to read
                inputFromExcel.put("NBKID",cell.toString()) ;
                row = sheet.getRow(ctr+1);
                cell = row.getCell(indexofColumn);
                inputFromExcel.put("Password", cell.toString());
                row = sheet.getRow(ctr+2);
                cell = row.getCell(indexofColumn);
                inputFromExcel.put("NBKIDEmail",cell.toString());
                row = sheet.getRow(ctr+3);
                cell = row.getCell(indexofColumn);
                inputFromExcel.put("sourceExcel" ,cell.toString());

            } catch(Exception e) {
            }

        return inputFromExcel;
    }

}
于 2018-03-19T07:23:30.883 回答
0
import java.util;

public class Main {
    public static void main(String[] args) {
        List<String> partyIdList = new ArrayList<String>();
        List<String> phNoList = new ArrayList<String>();
        String userName = System.getProperty("user.name");
        String path = "";
            try {
                Map data = new HashMap(UserInput.getUserInput());
                partyIdList = ReadExcel.getContentFromExcelSheet(data.get("sourceExcel").toString(),0);
                phNoList = ReadExcel.getContentFromExcelSheet(data.get("sourceExcel").toString(),1);
                WriteExcel.writeFile1(partyIdList, path,0,1);
                WriteExcel.writeFile1(phNoList,path,1,0);

            } catch (Exception e) {
                throw new RuntimeException("Error while read excel Sheet" + e);
            }

    }
}
于 2018-03-19T07:28:12.960 回答
-1
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ReadExcel {
    public static List getContentFromExcelSheet(String path ,int indexofColumn) throws InvalidFormatException, IOException {
        //System.out.println(path);
        List<String> ItemList = new ArrayList();
        InputStream inp = new FileInputStream(path);
        int ctr = 0;
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0); // Mention Sheet no. which you want to read
        Row row = null;
        Cell cell = null;
        boolean isNull = false;
        do{
            try{
                row = sheet.getRow(ctr);
                cell = row.getCell(indexofColumn); //Mention column no. which you want to read
                ItemList.add(cell.toString());
                //   System.out.println(cell.toString());
                ctr++;
            } catch(Exception e) {
                isNull = true;
            }

        }while(isNull!=true);
        inp.close();

        return ItemList;
    }
}
于 2018-03-19T09:22:44.423 回答