當前我正在使用下面的代碼將Xlsx文件轉換爲csv使用java.Its需要更多時間,因爲下面的代碼使用迭代器來執行此過程。但我需要任何其他更好的解決方案轉換XLSX使用到csv java.Please幫我如何使用java將xlsx文件轉換爲csv?
public class Test1{
static void convert(File inputFile, File outputFile) {
try {
FileOutputStream fos = new FileOutputStream(outputFile);
// Get the workbook object for XLSX file
XSSFWorkbook wBook = new XSSFWorkbook(
new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = wBook.getSheetAt(0);
Row row;
Cell cell;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + ",");
break;
case Cell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue() + ",");
break;
case Cell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue() + ",");
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + ",");
break;
default:
data.append(cell + ",");
}
}
}
fos.write(data.toString().getBytes());
fos.close();
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
// testing the application
public static void main(String[] args) {
// reading file from desktop
File inputFile = new File("D:\\Test.xlsx");
// writing excel data to csv
File outputFile = new File("D:\\Test1.csv");
convert(inputFile, outputFile);
}
}
感謝&問候, Tharanya乙
你希望它變得更快嗎?或者是什麼? – JIV
是的先生。進程應該很快 – Tharani