Poi读取excel并将内容写入文本
时间: 2023-10-18 09:16:36 浏览: 208
可以使用Apache POI读取Excel文件并将其写入文本文件中。以下是一个简单的Java代码示例:
```java
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelToTextConverter {
public static void main(String[] args) {
String excelFilePath = "path/to/excel/file.xlsx";
String textFilePath = "path/to/text/file.txt";
try {
Workbook workbook = new XSSFWorkbook(new FileInputStream(excelFilePath));
Sheet sheet = workbook.getSheetAt(0); // assuming you want to read the first sheet
BufferedWriter writer = new BufferedWriter(new FileWriter(textFilePath));
for (Row row : sheet) {
for (Cell cell : row) {
String cellValue = cell.getStringCellValue();
writer.write(cellValue + "\t"); // write cell value to text file with a tab delimiter
}
writer.newLine(); // write a new line separator after each row
}
writer.close();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在这个示例中,我们使用Apache POI的XSSFWorkbook类来读取Excel文件。然后,我们遍历工作表中的每一行和每一个单元格,并将单元格的字符串值写入文本文件中,使用制表符分隔每个单元格。最后,我们关闭写入器和工作簿。
阅读全文