java代码xlxs批量转csv
时间: 2023-07-30 17:10:10 浏览: 158
可以使用 Apache POI 库来读取 xlsx 文件,并将其转换为 csv 格式。以下是一个简单的示例代码:
```java
import java.io.File;
import java.io.FileWriter;
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.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class XLSXtoCSVConverter {
public static void main(String[] args) {
String folderPath = "C:/example/folder"; // xlsx 文件所在文件夹路径
File folder = new File(folderPath);
File[] files = folder.listFiles();
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".xlsx")) {
convertToCSV(file);
}
}
}
private static void convertToCSV(File file) {
try {
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> iterator = sheet.iterator();
FileWriter csvWriter = new FileWriter(file.getParent() + "/" + file.getName().replace(".xlsx", ".csv"));
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
StringBuilder sb = new StringBuilder();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if (currentCell.getCellType() == Cell.CELL_TYPE_STRING) {
sb.append(currentCell.getStringCellValue() + ",");
} else if (currentCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
sb.append(currentCell.getNumericCellValue() + ",");
}
}
sb.append("\n");
csvWriter.write(sb.toString());
}
workbook.close();
csvWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在上面的代码中,我们首先遍历文件夹中的所有 xlsx 文件,然后针对每个文件调用 `convertToCSV` 方法。在 `convertToCSV` 方法中,我们使用 Apache POI 库读取 xlsx 文件并遍历其所有单元格。对于每个单元格,我们将其值附加到一个 StringBuilder 中,最后将 StringBuilder 写入 csv 文件中。注意,我们使用 FileWriter 来写入 csv 文件。最后,我们需要关闭工作簿和 FileWriter。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)