溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-07-17 15:29:35 來(lái)源:億速云 閱讀:143 作者:小豬 欄目:編程語(yǔ)言

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

一、Excel的導(dǎo)入

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

方式一、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則有問(wèn)題,讀取不完整,將表頭的數(shù)目作為總的列數(shù),沒(méi)有的則補(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)建可寫(xiě)的工作薄
WritableWorkbook workbook =Workbook.createWorkbook(os);
//創(chuàng)建第一張表
WritableSheet sheet = workbook.createSheet("Sheet1",0);
//設(shè)置根據(jù)內(nèi)容自動(dòng)寬度
CellView cellView =newCellView();
 cellView.setAutosize(true);
//在下邊f(xié)or循環(huán)中為每一列設(shè)置

//設(shè)置列寬度,此種方式參數(shù)的意思,i-->對(duì)應(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)前列的寬度,文本太對(duì)會(huì)自動(dòng)換行 new Label(j, i+1, content[i][j-1],contentFormat);
WritableCellFormat contentFormat =newWritableCellFormat();
 contentFormat.setWrap(true);

//然后添加入庫(kù)信息條目
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)容寫(xiě)入到輸出流中,并關(guān)閉輸出流
workbook.write();
 workbook.close();
 os.close();

//將存儲(chǔ)了入庫(kù)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);
//每個(gè)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í)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI