使用poi读写xml
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
写excel
// 如果是xlsx文件,将HSSF改为XSSF
HSSFWorkbook hssfWorkbook=new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet();
HSSFRow row0 = sheet.createRow(0);
row0.createCell(0).setCellValue("ID");
row0.createCell(1).setCellValue("姓名");
row0.createCell(2).setCellValue("课程");
row0.createCell(3).setCellValue("分数");
File txtFile=new File("D:\\Eclipse\\2019.txt");
try {
BufferedReader reader=new BufferedReader(new FileReader(txtFile));
String textString=null;
int num=1;
while((textString=reader.readLine())!=null) {
HSSFRow row = sheet.createRow(num);
String[] split = textString.split(",");
for (int i = 0; i < split.length; i++) {
row.createCell(i).setCellValue(split[i]);
}
num++;
}
FileOutputStream outputStream=new FileOutputStream(new File("D:\\Eclipse\\excledemo.xls"));
hssfWorkbook.write(outputStream);
reader.close();
outputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
读excel
try {
FileInputStream excleInputStream=new FileInputStream(new File("D:\\Eclipse\\excledemo.xls"));
// 如果是xlsx文件,将HSSF改为XSSF
HSSFWorkbook hssfWorkbook2 = new HSSFWorkbook(excleInputStream);
HSSFSheet excleSheet = hssfWorkbook2.getSheetAt(0);
for (int i = 0; i < excleSheet.getPhysicalNumberOfRows(); i++) {
HSSFRow row = excleSheet.getRow(i);
for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
System.out.print(row.getCell(j).getStringCellValue()+" ");
}
System.out.println();
}
excleInputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2019.txt
exceldemo.xls