Java FileOutputStream(Apachhe POI)保存时间过长
当我使用ApachePOI编辑.xlsx文件时,保存时间太长。.xlsx文件包含公式、格式和冻结窗格。我正在使用以下代码Java FileOutputStream(Apachhe POI)保存时间过长,java,excel,apache-poi,xlsx,fileoutputstream,Java,Excel,Apache Poi,Xlsx,Fileoutputstream,当我使用ApachePOI编辑.xlsx文件时,保存时间太长。.xlsx文件包含公式、格式和冻结窗格。我正在使用以下代码 try { FileInputStream file = new FileInputStream(new File(path)); XSSFWorkbook fWorkbook = new XSSFWorkbook(file); XSSFSheet fSheet = fWorkbook.getSheetA
try {
FileInputStream file = new FileInputStream(new File(path));
XSSFWorkbook fWorkbook = new XSSFWorkbook(file);
XSSFSheet fSheet = fWorkbook.getSheetAt(0);
for(int i = 0; i < jTable1.getRowCount(); i++){
if(jTable1.getModel().getValueAt(i, index1).equals("1")){
XSSFCell cell = fSheet.getRow(i+1).getCell(index1);
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(new Double(1));
XSSFCellStyle cs = fWorkbook.createCellStyle();
cs.setDataFormat(fWorkbook.getCreationHelper().createDataFormat().getFormat("dd/MMMM/yyyy"));
cell =fSheet.getRow(i+1).getCell(index2);
cell.setCellValue(new Date());
cell.setCellStyle(cs);
}
}
file.close();
FileOutputStream fileOutputStream = new FileOutputStream(path);
fWorkbook.write(fileOutputStream);
fileOutputStream.close();
JOptionPane.showMessageDialog(this, "Data saved successfully.");
parent.removeContent();
}catch(Exception e){
e.printStackTrace();
}
试试看{
FileInputStream文件=新FileInputStream(新文件(路径));
XSSF工作簿fWorkbook=新XSSF工作簿(文件);
XSSFSheet fSheet=fWorkbook.getSheetAt(0);
对于(int i=0;i
编辑1:
excel文件:
从Excel将数据加载到JTable:
try {
FileInputStream file1 = new FileInputStream(new File("c:\\sample.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file1);
XSSFSheet sheet = workbook.getSheetAt(0);
int rowc=sheet.getLastRowNum()+1;
int colc=sheet.getRow(0).getLastCellNum();
Object heading[] = new Object[colc+1];
XSSFRow row1 = sheet.getRow(0);
for(int i=0, column =0; i < colc; i++){
if(i == 1){
heading[column++] = "";
}
heading[column++] = cellToString(row1.getCell(i));
}
Object [][]j=new Object[rowc-1][colc+1];
for (int i = 1; i < rowc; i++) {
row1 = sheet.getRow(i);
for (int jj = 0, column = 0; jj < colc; jj++) {
if(column == 1){
j[i-1][column++] = new Boolean(false);
j[i-1][column] = cellToString(row1.getCell(jj));
}
else{
j[i-1][column]=cellToString(row1.getCell(jj));
}
column++;
}
}
jTable1.setModel(new DefaultTableModel(j, heading){
public Class getColumnClass(int columnIndex) {
if(columnIndex == 0){
return java.lang.Integer.class;
}
else if(columnIndex == 1){
return java.lang.Boolean.class;
}
else{
return java.lang.Object.class;
}
}
});
jTable1.getColumnModel().getColumn(1).setMaxWidth(60);
jTable1.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
jTable1.getTableHeader().setReorderingAllowed(false);
} catch (IOException ex) {
JOptionPane.showMessageDialog(BarcodePrint.this, ex);
}
试试看{
FileInputStream文件1=新文件InputStream(新文件(“c:\\sample.xlsx”);
XSSF工作簿=新XSSF工作簿(文件1);
XSSFSheet sheet=workbook.getSheetAt(0);
int rowc=sheet.getLastRowNum()+1;
int colc=sheet.getRow(0.getLastCellNum();
对象标题[]=新对象[colc+1];
XSSFRow-row1=sheet.getRow(0);
for(int i=0,column=0;i
编辑列条形码打印后将数据写入excel:
try {
FileInputStream file = new FileInputStream(new File("c:\\sample.xlsx"));
XSSFWorkbook fWorkbook = new XSSFWorkbook(file);
XSSFCellStyle cs = fWorkbook.createCellStyle();
cs.setDataFormat(fWorkbook.getCreationHelper().createDataFormat().getFormat("dd/MMMM/yyyy"));
Date currentdate = new Date();
XSSFSheet fsheet = fWorkbook.getSheetAt(0);
Double barcodeprintstatus = new Double(1);
int newindex = 24;
int printdate = 26;
int uniqueid = 27;
for(int i = 0; i < jTable1.getModel().getRowCount(); i++){
if(jTable1.getModel().getValueAt(i, newindex).equals("1")){
for(int k=1; k < fsheet.getPhysicalNumberOfRows(); k++){
XSSFCell cell = fsheet.getRow(k).getCell(uniqueid-1);
String uid = cell.getRawValue();
if(jTable1.getModel().getValueAt(i, uniqueid).toString().equals(uid)){
cell = (fsheet.getRow(i+1)).getCell(newindex-1);
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(barcodeprintstatus);
cell = fsheet.getRow(i+1).getCell(printdate-1);
cell.setCellValue(currentdate);
cell.setCellStyle(cs);
}
}
}
}
file.close();
FileOutputStream fileOutputStream = new FileOutputStream("c:\\sample.xlsx");
fWorkbook.write(fileOutputStream); // this is taking so much of time. Approximately 1 min.
fileOutputStream.close();
}catch(Exception e){
e.printStackTrace();
}
试试{
FileInputStream文件=新的FileInputStream(新文件(“c:\\sample.xlsx”);
XSSF工作簿fWorkbook=新XSSF工作簿(文件);
XSSFCellStyle cs=fWorkbook.createCellStyle();
cs.setDataFormat(fWorkbook.getCreationHelper().createDataFormat().getFormat(“dd/MMMM/yyyy”);
日期currentdate=新日期();
XSSFSheet fsheet=fWorkbook.getSheetAt(0);
双条码打印状态=新双条码(1);
int newindex=24;
int printdate=26;
int uniqueid=27;
对于(int i=0;i
我无法解决这个问题<代码>fWorkbook.write(fileOutputStream)代码>占用了我上面提到的大量时间。请帮忙。是否有其他方法保存excel文件?或者我可以为单列rath部分写入数据吗
cell.setCellValue(new Double(1));
XSSFCellStyle cs = fWorkbook.createCellStyle();
cs.setDataFormat(fWorkbook.getCreationHelper().createDataFormat().getFormat("dd/MMMM/yyyy"));
cell.setCellValue(new Date());