我有一个Java应用程序,用于将hssf工作簿转换为xssf wokbook.workbook有预定义的模板。如果工作簿是EI想要将其从E.xls转换为E.xlsx,我能够做到我只是想将E.xls workbbok中的某些工作表转换为E.xlsx工作簿。面临的问题是,在最终的E.xlsx格式中要生成一个额外的工作表,即不需要,也没有填充:
public String convertXlsToXlsx(String sourceXlsFile,String destinationXlsxFile) {
log.info("Convertion from xls to xlsx Started...");
inpFn = sourceXlsFile;
// String xlsxIn = "D:\\arusha\\230029_06-30-2009_A.xlsx";
outFn = destinationXlsxFile;
try {
in = new BufferedInputStream(new FileInputStream(inpFn));
Workbook wbIn = new HSSFWorkbook(in);
File outF = new File(outFn);
FileInputStream input_document = new FileInputStream(
new File(outFn));
XSSFWorkbook outFn_workbook = new XSSFWorkbook(input_document);
int sheetCnt = wbIn.getNumberOfSheets();
System.out.println("The number of sheeetsss is"+sheetCnt);
for (int i = 0; i < sheetCnt; i++) {
Sheet sIn = wbIn.getSheetAt(i);
XSSFSheet outFnSheet = outFn_workbook.getSheetAt(i);
if (wbIn.isSheetHidden(i)) {
outFn_workbook.setSheetHidden(i, true);
continue;
}
if (wbIn.isSheetVeryHidden(i)) {
outFn_workbook.setSheetHidden(i, true);
continue;
}
Iterator<Row> rowIt = sIn.rowIterator();
while (rowIt.hasNext()) {
Row rowIn = rowIt.next();
Iterator<Cell> cellIt = rowIn.cellIterator();
while (cellIt.hasNext()) {
Cell cellIn = cellIt.next();
if (outFnSheet.getRow(cellIn.getRowIndex()) == null) {
continue;
}
Cell cellOut = outFnSheet.getRow(cellIn.getRowIndex())
.getCell(cellIn.getColumnIndex());
if (cellOut == null) {
continue;
}
if ((cellOut + "").trim().equalsIgnoreCase("X")
&& !(cellIn + "").trim().equalsIgnoreCase("X")) {
XSSFCellStyle cs = outFn_workbook.createCellStyle();
XSSFFont font = outFn_workbook.createFont();
font.setFontHeightInPoints((short) 6);
cs.setFont(font);
cellOut.setCellStyle(cs);
XSSFCellStyle cellstyle = outFn_workbook
.createCellStyle();
cellstyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
cellOut.setCellStyle(cellstyle);
}
switch (cellIn.getCellType()) {
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_BOOLEAN:
cellOut.setCellValue(cellIn.getBooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR:
cellOut.setCellValue(cellIn.getErrorCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
cellOut.setCellFormula(cellIn.getCellFormula());
break;
case Cell.CELL_TYPE_NUMERIC:
cellOut.setCellValue(cellIn.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
cellOut.setCellValue(cellIn.getStringCellValue());
break;
}
}
}
}
try {
// important to close InputStream
input_document.close();
// Open FileOutputStream to write updates
FileOutputStream output_file = new FileOutputStream(new File(
outFn));
// write changes
outFn_workbook.write(output_file);
System.out.println("sheet removed");
// close the stream
output_file.close();
// wbOut.write(out);
} catch (Exception e) {
log.error("XlsToXlsx:",e);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
log.error("XlsToXlsx:",e);
}
}
return outFn;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
我尝试使用removeSheetAt()方法删除工作表,但效果不理想。请帮助我。谢谢。