1
我是新來的Apache POI但不能對Java。我一直在試圖將數據從一個Excel文件轉換爲另一種,改變任何分隔符數據的新細胞。雖然大多數新的數據是正確的,但有一些例外沒有關注的空白單元格的規則。 這裏是輸入文件https://ufile.io/bce15 這裏是輸出文件https://ufile.io/55020的Java的Apache POI空白單元格
在圖像下面記錄4和5不遵守遊戲規則,我不能找出原因!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
public class testWrite {
public static void main(String [] args) {
int rowNums = 1;
try {
FileInputStream file = new FileInputStream(new File("ExceptionDataDummy.xlsx"));;
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet = wb.getSheetAt(0);
String filename = "ExceptionDataResult.xlsx" ;
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet1 = workbook.createSheet("FirstSheet");
Iterator<Row> rows = sheet.rowIterator();
int col = 0;
while(rows.hasNext()) {
XSSFRow row = (XSSFRow) rows.next();
XSSFRow row1 = sheet1.createRow((short)rowNums);
System.out.println("\n");
Iterator<Cell> cells = row.cellIterator();
while(cells.hasNext()) {
XSSFCell cell = (XSSFCell) cells.next();
if(XSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()) {
row1.createCell(col).setCellValue(cell.getNumericCellValue());
col++;
}
else
if(XSSFCell.CELL_TYPE_STRING==cell.getCellType()){
String contents = cell.getStringCellValue();
String[] items = contents.split(",");
for (String item : items) {
row1.createCell(col).setCellValue(item);
col++;
}
}
else
if(XSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType()) {
row1.createCell(col).setCellValue(cell.getBooleanCellValue());
col++;
}
else
if((cell.equals("")) || (XSSFCell.CELL_TYPE_BLANK==cell.getCellType()) || (cell == null)) {
cell.setCellType(Cell.CELL_TYPE_BLANK);
col++;
}
else {
System.out.print("Unknown cell type");
}
}
rowNums++;
col=0;
}
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");
wb.close();
workbook.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}