使用Java POI覆盖excel文件
我是Java POI新手,我正在尝试使用Java POI覆盖excel文件。让我明确一点,我不想每次构建代码时都打开一个新的.xls文件,但是我编写的代码就是这样做的。目的是,我将在excel上构建图表,并从数据库中读取图表的值,然后使用Java POI将其写入excel文件。以下是我的代码:使用Java POI覆盖excel文件,java,apache-poi,Java,Apache Poi,我是Java POI新手,我正在尝试使用Java POI覆盖excel文件。让我明确一点,我不想每次构建代码时都打开一个新的.xls文件,但是我编写的代码就是这样做的。目的是,我将在excel上构建图表,并从数据库中读取图表的值,然后使用Java POI将其写入excel文件。以下是我的代码: HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet firstSheet = workbook.createSheet("oldu"); HSSFS
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();
}
}
}
始终使用相同的文件名,当您运行该程序时,它将覆盖该文件 编辑
如果要修改现有excel文件,请查看并向下滚动到“读取或修改现有文件”部分。问题是Apache POI不支持excel文件格式的所有功能,包括图表。因此,无法使用POI生成图表,当使用POI打开现有Excel文件并对其进行修改时,Excel文件中的某些当前“对象”可能会丢失,因为POI无法处理它,并且在写入时,只写入生成的新信息和可以处理的现有信息 Apache认为这是POI的缺陷之一 我们对现有的Excel文件进行了类似的处理,将新数据填充到其中,但现有的Excel文件只包含格式样式,它们使用POI保存,但我认为图表存在很大问题。使用POI无法尝试填充数据以更新现有图表。
import org.apache.POI.openxml4j.exceptions.InvalidFormatException;
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();
}
}
}
导入org.apache.poi.ss.usermodel.*;
导入java.io.*;
导入java.util.List;
公共类WriteExcel{
公共静态void writeFile1(列表数据列表、字符串路径、int columnCount、int forwardIndex)引发InvalidFormatException、IOException{
试一试{
FileInputStream inputStream=新FileInputStream(新文件(路径));
工作簿=WorkbookFactory.create(inputStream);
工作表=工作簿。getSheetAt(0);
int rowCount=1;
对于(int i=0+forwardIndex;i
导入java.util;
公共班机{
公共静态void main(字符串[]args){
List partyIdList=new ArrayList();
List phNoList=new ArrayList();
字符串userName=System.getProperty(“user.name”);
字符串路径=”;
试一试{
Map data=newhashmap(UserInput.getUserInput());
partyIdList=ReadExcel.getContentFromExcelSheet(data.get(“sourceExcel”).toString(),0);
phNoList=ReadExcel.getContentFromExcelSheet(data.get(“sourceExcel”).toString(),1);
WriteExcel.writeFile1(partyIdList,路径,0,1);
WriteExcel.writeFile1(phNoList,path,1,0);
}捕获(例外e){
抛出新的运行时异常(“读取excel工作表时出错”+e);
}
}
}
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.poi.openxml4j.exceptions.InvalidFormatException;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.ss.usermodel.WorkbookFactory;
公共类ReadExcel{
公共静态列表getContentFromExcelSheet(字符串路径,int indexofColumn)引发InvalidFormatException,IOException{
//System.out.println(路径);
List ItemList=new ArrayList();
InputStream Input=新文件InputStream(路径);
int ctr=0;
工作簿wb=WorkbookFactory.create(inp);
Sheet Sheet=wb.getSheetAt(0);//提及要阅读的工作表编号
行=空;
Cell=null;
布尔值isNull=false;
做{
试一试{
row=sheet.getRow(ctr);
cell=row.getCell(indexofColumn);//提及要读取的列号
add(cell.toString());
//System.out.println(cell.toString());
ctr++;
}捕获(例外e){
isNull=true;
}
}while(isNull!=true);
inp.close();
返回项目列表;
}
}
所以您想
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;
}
}
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);
}
}
}
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;
}
}