關於java導入導出excel2007

由於公司之前用的導入導出excel只支持2003版本的(使用jxl插件)已經不能滿足客戶的需求,excel2003所能包括的最大數據行數遠低於excel2007(幾萬行和一百多萬的區別),所以需要升級導入導出插件是系統能支持excel2007。

我使用的是poi-3.8 jar包,可到http://download.csdn.net/detail/wang524506/4849282下載。

這裏介紹我所使用的插件poi,其中主要使用的是xssf。導出關鍵代碼如下(這裏數據源是resultset):


FileOutputStream fileOut = new FileOutputStream(path+file);//創建輸出流
if (rs == null || rs.size() == 0) {
System.out.println("null result, can't be exported.");
throw new Exception("null result, can't be exported.");
// return false;
}

XSSFWorkbook wb = new XSSFWorkbook();//2007格式
setExcelStyle(wb);// 執行樣式初始化

// 標題
String title = es.getSheetName();// p.getPropValue(request,"elasset","elasset.manage",Language);
XSSFSheet sheet = wb.createSheet(title);//2007格式


XSSFRow titleRow0 = sheet.createRow((short) 0);// 創建標題行
XSSFCell titleCell = titleRow0.createCell((short) 0); // 新建一個單元格
titleCell.setCellValue(title);//設置標題

XSSFRow titleRow = sheet.createRow((short) 1);// 創建第一行
XSSFRow titleRow2 = sheet.createRow((short) 2);// 創建第二行
titleRow.setHeightInPoints(20);// 20像素
int titleCount = es.getColusSize();// 列數
// 寫標題
for (int k = 0; k < titleCount; k++) {
XSSFCell cell = titleRow.createCell((short) k); // 新建一個單元格
XSSFCell cell2 = titleRow2.createCell((short) k); // 新建一個單元格
// XSSFCell cell = titleRow.createCell((short) k); //2007格式


// cell.setEncoding(HSSFCell.ENCODING_UTF_16); // 中文字符集轉換
cell.setCellStyle(titleStyle);// 設置標題樣式
cell2.setCellStyle(titleStyle);// 設置標題樣式
// cell.setCellValue(new HSSFRichTextString(titleStrs[k])); //
// 爲單元格賦值
// cell.setCellValue(wb.getCreationHelper().createRichTextString(""));
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell2.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(es.getFieldName(k));
cell2.setCellValue(es.getColusName(k));
sheet.setColumnWidth((short) k, (short) 5000);// 設置列寬
}

int index = 0;
int rownum = 2;
// System.out.println(r.length);
// String tmp = "";


String tmpStr;
int colus = es.getColusSize();// fieldsName.length;//rs.getHashColus().size();
while (rs.next()) {
XSSFRow row = sheet.createRow((short) (++rownum)); // 新建一行
for (int j = 0; j < colus; j++) {
XSSFCell cell = row.createCell((short) j); // 新建一個單元格
tmpStr = es.getFixedValue(j);
if (tmpStr == null)// 導出時,有指定固定值則導固定值
{
tmpStr = rs.getString(es.getFieldName(j));
}
if (es.isNumeric(j))
{
try 
    {
cell.setCellValue(Double.parseDouble(tmpStr));
}
  catch(NumberFormatException e)
    { //System.out.println("出現轉換異常");
     //System.out.println(e.getMessage());
//      worksheet.addCell(new Number(j, index + 3, 0.00));
  cell.setCellValue(0.00);  
    }
 //e.printStackTrace(); }
 //  finally{ System.out.println("finally!"); }

}

else
cell.setCellValue(tmpStr);
// worksheet.addCell(new Label(j, index + 3, tmpStr, wcfFC));
}
index++;
}
rs.setCursor(1);
updateCount = rs.size();

} catch (Exception e) {
throw e;
} finally {
wb.write(fileOut);
fileOut.flush();
fileOut.close();
}
return true;
}


導入數據關鍵代碼如下(傳入excel2007格式文件,返回ArrayList,傳入數據的文件格式由配置文件設置,這裏不詳細描述):

ArrayList arrkpoolinitList = new ArrayList();
StringBuffer sb = new StringBuffer();
Workbook wb = null;
try {
// 構造Workbook(工作薄)對象
wb = WorkbookFactory.create(new FileInputStream(new File(path)));
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 獲得了Workbook對象之後,就可以通過它得到Sheet(工作表)對象了
Sheet rs = wb.getSheetAt(0);
  int startRowNum = rs.getFirstRowNum();//得到開始行
int endRowNum = rs.getLastRowNum();//得到結束行
int rsColumns = rs.getRow(1).getLastCellNum()-rs.getRow(1).getFirstCellNum();//得到第一行列數
int rowNum = endRowNum-startRowNum+1;//得到行數
//Sheet[] sheet = wb.getSheet();
//if (sheet != null && sheet.length >0) {
// 對每個工作表進行循環
//for (int i = 0; i < sheet.length; i++) {
// 得到當前工作表的行數
//int rowNum = sheet[i].getRows();
int startLine=1;
try {
hbsXml xmlBean = new hbsXml();
startLine=Integer.parseInt(xmlBean.getText("goldentaxImportConfig.xml","startLine"));
if(txtlinkfile!=null)
{
if(txtlinkfile.equals("yes"))//導入LinkFile
{
     startLine=Integer.parseInt(xmlBean.getText("goldentaxImportConfigTmp.xml","startLine"));
}
}
} catch (Exception e) {
startLine=1;
}

for (int j = startLine; j < rowNum; j++) {
// 得到當前行的所有單元格
int columns = rs.getRow(j).getLastCellNum()-rs.getRow(j).getFirstCellNum();//得到第j行列數
Row row = rs.getRow(j);
Kp_pool_Init kppoolinit = new Kp_pool_Init();
if (columns > 0) {
// 對每個單元格進行循環

   if(importLinepojo.getDoc_NoNum()!=-1){
    row.getCell(importLinepojo.getDoc_NoNum()).setCellType(Cell.CELL_TYPE_STRING);
    kppoolinit.setDoc_No(row.getCell(importLinepojo.getDoc_NoNum()).getStringCellValue());
   }
   if(importLinepojo.getSpecNum()!=-1){
    row.getCell(importLinepojo.getSpecNum()).setCellType(Cell.CELL_TYPE_STRING);
    kppoolinit.setSpec(row.getCell(importLinepojo.getSpecNum()).getStringCellValue());
   }
。。。。。。。。。。。。。。。。。
}
arrkpoolinitList.add(kppoolinit);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章