本篇要點:
1、文件讀寫的基本操作
2、字符串分割
3、POI生成Excel文件(.xlsx)
需求:
將csv格式的數(shù)據(jù)文件的內(nèi)容導(dǎo)入到表格中吞彤,生成xlsx格式的文件。
(這是個很簡單的任務(wù)叹放,這里記錄一下饰恕,以便以后復(fù)用代碼。)
轉(zhuǎn)換前:
轉(zhuǎn)換后:
起因:
思路:
整體思路
1井仰、按行讀取文件數(shù)據(jù)埋嵌,過濾掉不必要的行。
2俱恶、有效行進行數(shù)據(jù)分割雹嗦。
3范舀、分割好的數(shù)據(jù)存入表格的單元格。
4俐银、輸出生成xlsx文件尿背。
用記事本打開csv文件分析一下,發(fā)現(xiàn)需要解決的問題主要有:
1捶惜、開頭有多余代碼
2田藐、有重復(fù)的標題行,每行末尾有無效數(shù)據(jù)(,,,,)
讀取行的詳細思路
1吱七、正則匹配“+”開頭的行(稱為“+”行)并計數(shù)汽久,只有讀取“+”行計數(shù)為1時,置布爾值title_read為true踊餐,表示下一行可以讀取為標題行景醇。
2、當“+”行計數(shù)>=1時吝岭,讀取的非“+”行才是有效行三痰,這樣就可以忽略開頭代碼。
3窜管、當“+”行計數(shù)散劫!=1時,置title_read為false幕帆,表示之后不再讀取標題行获搏。無效標題行的判斷條件為title_read為false時“+”行計數(shù)為基數(shù)(因為每個標題行前后都有一行“+”行)。這樣就可以忽略掉多余標題行失乾。
4常熙、進行字符串切割后,不輸出第一個(第一個“|”前是空字符串)和最后一個切割數(shù)據(jù)(“,,,,,”無效數(shù)據(jù))碱茁。
完整代碼
因為這是個人筆記裸卫,就不細講每一步的實現(xiàn)了,代碼比較簡單纽竣,細節(jié)見注釋彼城。
package excelOP;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
public class ExcelOp {
public void trans(String file_in,String file_out//參數(shù)file_in,file_out分別為輸入文件路徑和輸出文件路徑
){
/*實例化輸入、輸出流*/
File f_in=new File(file_in);//輸入文件
FileInputStream ips=null;
InputStreamReader ipsr=null;
File f_out=new File(file_out);//輸出文件
FileOutputStream ops=null;
try {
ips=new FileInputStream(f_in);
ipsr=new InputStreamReader(ips);
ops=new FileOutputStream(f_out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*創(chuàng)建excel表格文件并輸出*/
//創(chuàng)建工作薄
SXSSFWorkbook wb = new SXSSFWorkbook();
try {
//創(chuàng)建新的一頁
SXSSFSheet sheet = wb.createSheet("new sheet");
BufferedReader br=new BufferedReader(ipsr);
int count_title_framelines=0;
boolean title_read=false;
String valueString = null;
int count_line=0;//表格的有效行計數(shù)
while ((valueString=br.readLine())!=null){//循環(huán)處理每一行
if(valueString.matches("\\+.+")){//正則匹配標題前后行“+”開頭的行
count_title_framelines++;
if(count_title_framelines==1)title_read=true;
else title_read=false;
}
else{//非“+”開頭的行
if(!(title_read==false&&count_title_framelines%2==1)//忽略重復(fù)標題行
&&count_title_framelines>=1){//忽略開頭代碼
//分解每一行有效行
String[] strs_cell=valueString.split("\\|");//用“|”切割每一行作為一個單元格數(shù)據(jù)
SXSSFRow row = sheet.createRow((short)count_line);
for(int i=1;i<strs_cell.length-1;++i){//循環(huán)每個切割得到的數(shù)據(jù)退个,除了最后一個(,,,)和第一個(空)
//創(chuàng)建要顯示的內(nèi)容,創(chuàng)建一個單元格募壕,第一個參數(shù)為列坐標,第二個參數(shù)為行坐標语盈,第三個參數(shù)為內(nèi)容
row.createCell((short)i-1).setCellValue(strs_cell[i]);
System.out.println("正在導(dǎo)入第"+count_line+"行……");
}
count_line++;
}
}
}
//把創(chuàng)建的表格寫入到輸出流中舱馅,并關(guān)閉輸出流
wb.write(ops);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(wb!=null){
try {
wb.close();
System.out.println("寫入表格完畢!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ips!=null){
try {
ips.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ops!=null){
try {
ops.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ExcelOp exop=new ExcelOp();
exop.trans("Ngap.csv", "test.xlsx");
}
}
參考:
POI操作Excel常用方法總結(jié)
注:這篇主要講的是用HSSF生成xls格式的文件(Excel2003版之前用的格式)刀荒,生成xlsx用的是SXSSF代嗤,如上個部分的代碼棘钞。不過相應(yīng)的操作大同小異,把所有的“HSSF”改成“SXSSF”即可干毅,相應(yīng)的包可以到Apache官網(wǎng)下載宜猜,給個鏈接:Apache POI - Download Release Artifacts