这是用java语言实现Excel文件转csv文件的简单小列子
package com.csv;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import com.monitorjbl.xlsx.StreamingReader;
//main 函数测试方法
public class CsvUtil {
public static void main(String[] args) {
csvs();
}
//实现由Excel转csv的功能
public static void csvs(){
BufferedWriter bw=null;
Workbook wb=null;
try {
//加载Excel文件设置加载的缓存
wb=StreamingReader.builder().bufferSize(4096).rowCacheSize(200).open(new FileInputStream("E://data/book.xlsx"));
//设置csv转换储存的目标绝对路径,并设置编码
bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D://javalixiaobiao/MySQL/MySQL Server 5.6/Uploads/temp.csv"),"utf-8") );
//读取文件信息
Sheet sheetAt = wb.getSheetAt(0);
for (Row row : sheetAt) {
for (Cell cell : row) {
if(cell.getCellTypeEnum()==CellType.STRING){
bw.write(cell.getStringCellValue());
}
if(cell.getCellTypeEnum()==CellType.NUMERIC){
bw.write(""+cell.getNumericCellValue());
}
if(cell.getCellTypeEnum()==CellType.BOOLEAN){
bw.write(""+cell.getBooleanCellValue());
}
}
bw.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}//关流
finally{
if(null!=wb){
try {
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
if(null!=bw){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/16896.html