java代码xlxs转csv
时间: 2023-07-30 16:10:10 浏览: 142
java xlsx转csv
5星 · 资源好评率100%
以下是使用Apache POI库将XLSX文件转换为CSV格式的Java代码示例:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
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.xssf.usermodel.XSSFWorkbook;
public class XLSXtoCSVConverter {
public static void convert(File inputFile, File outputFile) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(inputFile));
FileWriter csvWriter = new FileWriter(outputFile);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
csvWriter.append(cell.getStringCellValue());
break;
case NUMERIC:
csvWriter.append(String.valueOf(cell.getNumericCellValue()));
break;
case BOOLEAN:
csvWriter.append(String.valueOf(cell.getBooleanCellValue()));
break;
case BLANK:
csvWriter.append("");
break;
default:
csvWriter.append("");
break;
}
csvWriter.append(",");
}
csvWriter.append("\n");
}
workbook.close();
csvWriter.flush();
csvWriter.close();
}
}
```
通过调用convert()方法,并传入XLSX文件和CSV输出文件的路径,即可将XLSX文件转换为CSV格式。
阅读全文