자바 엑셀 가 져 오기,내 보 내기 실현

8186 단어 자바Excel
엑셀 가 져 오기
가 져 오기 에는 두 가지 방식 이 있 는데 하 나 는 JXL 이 고 다른 하 나 는 POI 이지 만 전 자 는 높 은 버 전의 Excel(07 이상)을 읽 을 수 없고 후 자 는 더욱 호환성 이 있다.두 가지 방식 을 모두 시 도 했 기 때문에 모두 붙 여 공유 합 니 다(잘못 되면 지적 해 주세요)
방식 1.JXL 가 져 오기  필요 한 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);
//      
int columns = sheet.getColumns();
//      
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;
}
방식 2.POI 가 져 오기 
필요 한 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);
//    
int rows = sheet.getLastRowNum();
//    --->     null    ,     ,            ,      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;
}
//       
// 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;
} 
엑셀 내 보 내기
JXL 로 구현

publicstaticvoid creatExcel(List<PutStorageInfo> storageInfoList,String fileName){
try{
OutputStream os =newFileOutputStream(fileName);
//        
WritableWorkbook workbook =Workbook.createWorkbook(os);
//      
WritableSheet sheet = workbook.createSheet("Sheet1",0);
//          
CellView cellView =newCellView();
 cellView.setAutosize(true);
//   for         

//     ,         ,i-->       j-->      
// sheet.setColumnView(0, 100);
// sheet.setRowView(0, 300);
//              
WritableFont boldFont =newWritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//   
WritableCellFormat cellrFormate =newWritableCellFormat(boldFont);
 cellrFormate.setBackground(Colour.YELLOW);
//     
List<String> titleList = getTitleList();
//       ,    
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);
//  content          ,          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);
}
}

//             ,      
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    11 
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;
}
 이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기