java如何實(shí)現(xiàn)Excel的導(dǎo)入、導(dǎo)出操作

這篇文章主要為大家展示了java如何實(shí)現(xiàn)Excel的導(dǎo)入、導(dǎo)出操作,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

在海豐等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),營銷型網(wǎng)站建設(shè),外貿(mào)網(wǎng)站建設(shè),海豐網(wǎng)站建設(shè)費(fèi)用合理。

一、Excel的導(dǎo)入

導(dǎo)入可采用兩種方式,一種是JXL,另一種是POI,但前者不能讀取高版本的Excel(07以上),后者更具兼容性。由于對兩種方式都進(jìn)行了嘗試,就都貼出來分享(若有錯誤,請給予指正)

方式一、JXL導(dǎo)入  所需jar包 JXL.jar

publicstaticList<PutStorageInfo> readExcelByJXL(String filePath){
List<PutStorageInfo> infoList =newArrayList<PutStorageInfo>();
Map<String,List<String>> map =newHashMap<String,List<String>>();
 infoList.clear();
try{
InputStream is =newFileInputStream(filePath);
Workbook workbook =Workbook.getWorkbook(is);
//獲取第1張表
Sheet sheet = workbook.getSheet(0);
//獲取總的列數(shù)
int columns = sheet.getColumns();
//獲取總的行數(shù)
int rows = sheet.getRows();
//先列后行(j,i)
for(int i =1; i < rows; i++){
List<String> contentList =newArrayList<String>();
 contentList.clear();
for(int j =1; j < columns; j++){
 contentList.add(sheet.getCell(j,i).getContents());
}
 map.put("StorageInfo"+i, contentList);
}

//遍歷map集合,封裝成bean
for(Map.Entry<String,List<String>> entry : map.entrySet()){
List<String> list = entry.getValue();
PutStorageInfo storageInfo =newPutStorageInfo();
 storageInfo.setProductcode(list.get(0));
 storageInfo.setProductsort(list.get(1));
 storageInfo.setProductbrand(list.get(2));
 storageInfo.setProductname(list.get(3));
 storageInfo.setProductquantity(list.get(4));
 storageInfo.setProductcontent(list.get(5));
 storageInfo.setProductnetweight(list.get(6));
 storageInfo.setProductcountry(list.get(7));
 storageInfo.setProductpdate(list.get(8));
 storageInfo.setProductprice(list.get(9));
 storageInfo.setProductmark(list.get(10));

 infoList.add(storageInfo);
}
 is.close();
}catch(Exception e){
 e.printStackTrace();
}
return infoList;
}

方式二、POI導(dǎo)入 

所需jar包
poi-3.6-20091214.jar
poi-ooxml-3.6-20091214.jar
poi-ooxml-schemas-3.6-20091214.jar
xmlbeans-2.3.0.jar
dom4j-1.6.1.jar
jdom-2.0.6.jar

publicstaticList<PutStorageInfo> readExcelByPOI(String filePath){
List<PutStorageInfo> infoList =newArrayList<PutStorageInfo>();
Map<String,List<String>> map =newHashMap<String,List<String>>();
 infoList.clear();
try{
InputStream is =newFileInputStream(filePath);

int index = filePath.lastIndexOf(".");
String postfix = filePath.substring(index+1);

Workbook workbook =null;
if("xls".equals(postfix)){
 workbook =newHSSFWorkbook(is);
}elseif("xlsx".equals(postfix)){
 workbook =newXSSFWorkbook(is);
}
//獲取第1張表
Sheet sheet = workbook.getSheetAt(0);
//總的行數(shù)
int rows = sheet.getLastRowNum();
//總的列數(shù)--->最后一列為null則有問題,讀取不完整,將表頭的數(shù)目作為總的列數(shù),沒有的則補(bǔ)為null
int columns = sheet.getRow(0).getLastCellNum();
//先列后行
for(int i =1; i <= rows; i++){
  Row row = sheet.getRow(i);
 if(null!= row && row.getFirstCellNum()==-1){//這一行是空行,不讀取
 continue;
}
//這一行的總列數(shù)
// columns = row.getLastCellNum();
List<String> contentList =newArrayList<String>();
 contentList.clear();
for(int j =1; j < columns; j++){
if(row.getCell(j)!=null){
 row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
 contentList.add(row.getCell(j).getStringCellValue());
}else{
 contentList.add("");
}
}
 map.put("StorageInfo"+i, contentList);
}

//遍歷map集合,封裝成bean
for(Map.Entry<String,List<String>> entry : map.entrySet()){
List<String> list = entry.getValue();
PutStorageInfo storageInfo =newPutStorageInfo();
 storageInfo.setProductcode(list.get(0));
 storageInfo.setProductsort(list.get(1));
 storageInfo.setProductbrand(list.get(2));
 storageInfo.setProductname(list.get(3));
 storageInfo.setProductquantity(list.get(4));
 storageInfo.setProductcontent(list.get(5));
 storageInfo.setProductnetweight(list.get(6));
 storageInfo.setProductcountry(list.get(7));
 storageInfo.setProductpdate(list.get(8));
 storageInfo.setProductprice(list.get(9));
 storageInfo.setProductmark(list.get(10));

 infoList.add(storageInfo);
}
 is.close();
}catch(Exception e){
 e.printStackTrace();
}

return infoList;
} 

二、Excel導(dǎo)出

采用JXL實(shí)現(xiàn)

publicstaticvoid creatExcel(List<PutStorageInfo> storageInfoList,String fileName){
try{
OutputStream os =newFileOutputStream(fileName);
//創(chuàng)建可寫的工作薄
WritableWorkbook workbook =Workbook.createWorkbook(os);
//創(chuàng)建第一張表
WritableSheet sheet = workbook.createSheet("Sheet1",0);
//設(shè)置根據(jù)內(nèi)容自動寬度
CellView cellView =newCellView();
 cellView.setAutosize(true);
//在下邊f(xié)or循環(huán)中為每一列設(shè)置

//設(shè)置列寬度,此種方式參數(shù)的意思,i-->對應(yīng)的行或列 j-->要設(shè)置的寬度
// sheet.setColumnView(0, 100);
// sheet.setRowView(0, 300);
//設(shè)置字體加粗且背景顏色為黃色
WritableFont boldFont =newWritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//黑體 
WritableCellFormat cellrFormate =newWritableCellFormat(boldFont);
 cellrFormate.setBackground(Colour.YELLOW);
//先添加表頭
List<String> titleList = getTitleList();
//循環(huán)創(chuàng)建單元格,先列后行
for(int i =0; i < titleList.size(); i++){
//sheet.setColumnView(i, cellView);
 sheet.setColumnView(i,20);

Label label =newLabel(i,0, titleList.get(i), cellrFormate);
 sheet.addCell(label);
}

LogUtil.logOut(JXLWriteExcel.class,storageInfoList.size()+"");

String[][] content = convertToArr(storageInfoList);
//設(shè)置content的自適應(yīng)當(dāng)前列的寬度,文本太對會自動換行 new Label(j, i+1, content[i][j-1],contentFormat);
WritableCellFormat contentFormat =newWritableCellFormat();
 contentFormat.setWrap(true);

//然后添加入庫信息條目
for(int i =0; i < storageInfoList.size(); i++){
Label labelID =newLabel(0,i+1,(i+1)+"");
 sheet.addCell(labelID);

for(int j =1; j < titleList.size(); j++){
Label label =newLabel(j, i+1, content[i][j-1]);
 sheet.addCell(label);
}
}

//把創(chuàng)建的內(nèi)容寫入到輸出流中,并關(guān)閉輸出流
workbook.write();
 workbook.close();
 os.close();

//將存儲了入庫bean的list清空
storageInfoList.clear();

}catch(Exception e){
 e.printStackTrace();
}
}

privatestaticString[][] convertToArr(List<PutStorageInfo> storageInfoList){
String[][] content =newString[storageInfoList.size()][11];
for(int i =0; i < storageInfoList.size(); i++){
PutStorageInfo info = storageInfoList.get(i);
//每個bean中總項(xiàng)有11項(xiàng)
content[i][0]= info.getProductcode();
 content[i][1]= info.getProductsort();
 content[i][2]= info.getProductbrand();
 content[i][3]= info.getProductname();
 content[i][4]= info.getProductquantity();
 content[i][5]= info.getProductcontent();
 content[i][6]= info.getProductnetweight();
 content[i][7]= info.getProductcountry();
 content[i][8]= info.getProductpdate();
 content[i][9]= info.getProductprice();
 content[i][10]= info.getProductmark();
}
return content;

}

privatestaticList<String> getTitleList(){
List<String> list =newArrayList<String>();
 list.add("Item No.");
 list.add("Product code");
 list.add("Sort");
 list.add("Brand");
 list.add("Product Name");
 list.add("Quantity(Pieces)");
 list.add("Content");
list.add("Net Weight");
 list.add("Country");
 list.add("Best before date");
 list.add("Price(EURO)");
 list.add("Remarks");

return list;
}

以上就是關(guān)于java如何實(shí)現(xiàn)Excel的導(dǎo)入、導(dǎo)出操作的內(nèi)容,如果你們有學(xué)習(xí)到知識或者技能,可以把它分享出去讓更多的人看到。

文章標(biāo)題:java如何實(shí)現(xiàn)Excel的導(dǎo)入、導(dǎo)出操作
轉(zhuǎn)載源于:http://muchs.cn/article14/pispge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、營銷型網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、網(wǎng)站策劃用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司