使用POI實現報表打印功能


[版權申明:本文系做者原創,轉載請註明出處]
文章出處:http://blog.csdn.net/sdksdk0/article/details/53393453
做者:朱培 ID:sdksdk0 java


這篇文章主要分享的是使用apache的poi來實現數據導出到excel的功能,這裏提供三種解決方案。你可使用最原始最簡單的一步步添加樣式或者數據,你也能夠經過一個模板來進行模板化的導出,也能夠對百萬級數據進行處處。如今不少人提供導出功能是不支持大數據量的導出的,我記得有的朋友導出3-4萬條數據系統就掛掉了。因此對於大量數據導出,本文也提供解決方案。git

一、POI解決的問題

關於POI的簡介,我這裏就再也不說起,你能夠參考這篇文章:http://blog.csdn.net/sdksdk0/article/details/52557755。github

JXL,POI都是操做excel
Excel一些企業小的應用都直接用excel來實現,例如:工資報表,人員名單,進銷存
做爲數據的備份和恢復(導入、導出)sql

Jxl它只能操做excel 2003版本,它導入導出數據量小時性能很高
POI 它能夠操做office系列軟件word、excel、ppt、visio(畫網絡佈局、家裝),在早期版本中它在導出海量數據時,容易崩潰。在新版本中它解決了這個海量數據時,進行了優化,解決了這個問題。咱們如今通常是使用3.0版本以後的。數據庫

目前常見讀寫Excel的工具類開源javaAPI有兩種方式,
一個是JXL(Java Excel API) 官網地址:http://jexcelapi.sourceforge.net/
一個是Apache的POI(Poor Obfuscation Implementation)官網地址:http://poi.apache.org/apache

POI支持微軟的OLE2格式文件Office 2003及如下版本;同時支持微軟的OOXML(Office Open XML)標準,也就是Office 2007以上版本。JXL只能實現對Excel 2003如下版本的支持。api

POI使用HFFS對象操做OLE2格式Excel,文件後綴爲.xls的;使用XSSF、SXSSF對象操做OOXML格式Excel,文件後綴爲.xlsx的。markdown

對於OLE2版本的Excel,一個Sheet工做表它的行最多支持到65536行,列支持到256列;
對於OOXML版本的Excel,一個Sheet工做表它的行支持到1048576行,列支持到65536列。網絡

2.POI的入門

固然咯,若是你的是maven工程,須要先把座標導進來。否則怎麼運行呢,你說是吧,哈哈!app

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

若是你的是普通的工程,那麼本身導個jar包進去吧!

使用poi只須要7個步驟,很是簡單。

//基本使用

@Test
    public void HSSF() throws IOException{
        //1.建立一個工做簿
        Workbook wb=new HSSFWorkbook();
        //2.建立一個工做表sheet
        Sheet  sheet=wb.createSheet();
        //3.建立一個行對象
        Row  nRow=sheet.createRow(4); //從0開始
        //四、建立一個單元格對象,指定列
        Cell nCell=nRow.createCell(4);
        //五、設置內容
        nCell.setCellValue("指令匯科技");
        //6.保存
        OutputStream stream=new FileOutputStream(new File("D:\\test1.xls")); 
        wb.write(stream);
        //7.關閉
        stream.close();

    }

前面的這種方式導出來的excel表格都是很是原始的,很是簡陋的,那麼咱們想添加一些樣式怎麼辦呢?
so,咱們可使用下面這種方式:咱們可使用CellStyle來設置樣式。

@Test
    public void HSSF1() throws IOException{
        //1.建立一個工做簿
        Workbook wb=new HSSFWorkbook();
        //2.建立一個工做表sheet
        Sheet  sheet=wb.createSheet();
        //3.建立一個行對象
        Row  nRow=sheet.createRow(4); //從0開始
        //四、建立一個單元格對象,指定列
        Cell nCell=nRow.createCell(4);
        //五、設置內容
        nCell.setCellValue("指令匯科技");


        CellStyle titleStyle=wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("微軟雅黑");  //設置字體類型
        font.setFontHeightInPoints((short) 26);  //設置字體大小
        titleStyle.setFont(font);

        nCell.setCellStyle(titleStyle);
        //6.保存
        OutputStream stream=new FileOutputStream(new File("D:\\test1.xls")); 
        wb.write(stream);
        //7.關閉
        stream.close();

    }

3.報表打印

說到這裏,我就須要打印一個以下圖所示的表。
這裏寫圖片描述

這些數據我都是從數據庫裏面查詢出來的,和個人業務是有關係的咯,這個如何從數據庫裏面把這些數據查出來我就不說了,這裏假設你已經有這些數據了,(真正感興趣的,能夠去下載個人這個項目源碼查看哦,文末提供下載地址)。

關於把這個報表打印出來,這裏我提供3種方式

方案1

就是直接在代碼裏面設置表格的列寬、樣式等。

一、建立一個工做簿:

 Workbook wb=new HSSFWorkbook();

二、…反正就是前面說的那7個步驟啦!。

這裏我要說的是添加樣式。

對於大標題:例如我這裏是2016年11月出貨表。

//大標題的樣式
    public CellStyle bigTitle(Workbook wb,CellStyle nStyle,Font font){
        font.setFontName("宋體");
        font.setFontHeightInPoints((short) 16); 
        //字體加粗
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //橫向居中
        nStyle.setAlignment(CellStyle.ALIGN_CENTER);
        //縱向居中
        nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);     //單元格垂直居中

        nStyle.setFont(font);
        return nStyle;
    }

而後代在碼中進行調用:

//大標題,合併單元格
        sheet.addMergedRegion(new CellRangeAddress(0,0,1,9)); //開始行,結束行,開始列,結束列
        //合併單元格的內容寫在合併前第一個單元格中
        nRow=sheet.createRow(rowNo++);

        nRow.setHeightInPoints(36);

        nCell=nRow.createCell(1);
        nCell.setCellValue(inputDate.replace("-0", "年").replaceFirst("-", "年")+"月份出貨表");
        nCell.setCellStyle(this.bigTitle(wb, nStyle, font));

而後是標題欄,就是個人那個客戶、訂單號這些內容哈!

//標題樣式
    public CellStyle Title(Workbook wb,CellStyle nStyle,Font font){
        font.setFontName("黑體");
        font.setFontHeightInPoints((short) 12); 

        //橫向居中
        nStyle.setAlignment(CellStyle.ALIGN_CENTER);
        //縱向居中
        nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //單元格垂直居中

        //表格線
        nStyle.setBorderTop(CellStyle.BORDER_THICK); //粗實線
        nStyle.setBorderBottom(CellStyle.BORDER_THIN); //實線
        nStyle.setBorderLeft(CellStyle.BORDER_THIN); 
        nStyle.setBorderRight(CellStyle.BORDER_THIN); 

        nStyle.setFont(font);
        return nStyle;
    }

在代碼中調用:

String[] title=new     String[]{"客戶","訂單號","貨號","數量","工廠","附件","工廠交期","船期","貿易條款"  };

        nRow=sheet.createRow(rowNo++);
        nRow.setHeightInPoints(26.25f);

        //初始化
        nStyle=wb.createCellStyle();
        font=wb.createFont();

        for(int  i=0;i<title.length;i++){
            nCell=nRow.createCell(i+1);
            nCell.setCellValue(title[i]);
            nCell.setCellStyle(this.Title(wb, nStyle, font));

        }

接下來是內容,這裏的填充數據都是從數據庫中查詢出來的:

//文字樣式
    public CellStyle Text(Workbook wb,CellStyle nStyle,Font font){
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short) 10); 

        //橫向居中
        nStyle.setAlignment(CellStyle.ALIGN_CENTER);
        //縱向居中
        nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //單元格垂直居中

        //表格線

        nStyle.setBorderBottom(CellStyle.BORDER_THIN); //實線
        nStyle.setBorderLeft(CellStyle.BORDER_THIN); 
        nStyle.setBorderRight(CellStyle.BORDER_THIN); //實線

        nStyle.setFont(font);
        return nStyle;
    }

填充數據:

//初始化
        nStyle=wb.createCellStyle();
        font=wb.createFont();

        //換行
        for(int j=0;j<dataList.size();j++){
            OutProductVO op=dataList.get(j);
            colNo=1;

            nRow=sheet.createRow(rowNo++);

            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getCustomName());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getProductNo());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getContractNo());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getCnumber());
            nCell.setCellStyle(this.Text(wb, nStyle, font));



            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getFactoryName());
            nCell.setCellStyle(this.Text(wb, nStyle, font));



            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getExts());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getDeliveryPeriod());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getSpipTime());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getTradeTerms());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


        }

最後提供一個下載的方法:

//下載
        DownloadUtil dUtil=new DownloadUtil();
        ByteArrayOutputStream os=new ByteArrayOutputStream();
        wb.write(os);
        dUtil.download(os, response, "出貨表.xls");

完整代碼以下:

//方案1
    @RequestMapping("/cargo/outproduct/print.action")
    public void print(String inputDate,HttpServletResponse  response) throws IOException{
        List<OutProductVO> dataList = outProductService.find(inputDate);

        Workbook wb=new HSSFWorkbook();
        Sheet sheet = wb.createSheet();
        Row nRow=null;
        Cell nCell=null;

        //行號
        int  rowNo=0;
        //列號
        int colNo=1;

        //聲明樣式對象和字體對象
        CellStyle nStyle=wb.createCellStyle();
        Font font = wb.createFont();

        //列寬
        sheet.setColumnWidth(0,2*300);
        sheet.setColumnWidth(1,26*300);
        sheet.setColumnWidth(2,12*300);
        sheet.setColumnWidth(3,29*300);
        sheet.setColumnWidth(4,10*300);
        sheet.setColumnWidth(5,12*300);
        sheet.setColumnWidth(6,8*300);
        sheet.setColumnWidth(7,10*300);
        sheet.setColumnWidth(8,10*300);
        sheet.setColumnWidth(9,9*300);



        //大標題,合併單元格
        sheet.addMergedRegion(new CellRangeAddress(0,0,1,9)); //開始行,結束行,開始列,結束列
        //合併單元格的內容寫在合併前第一個單元格中
        nRow=sheet.createRow(rowNo++);

        nRow.setHeightInPoints(36);

        nCell=nRow.createCell(1);
        nCell.setCellValue(inputDate.replace("-0", "年").replaceFirst("-", "年")+"月份出貨表");
        nCell.setCellStyle(this.bigTitle(wb, nStyle, font));


        String[] title=new  String[]{"客戶","訂單號","貨號","數量","工廠","附件","工廠交期","船期","貿易條款"  };

        nRow=sheet.createRow(rowNo++);
        nRow.setHeightInPoints(26.25f);

        //初始化
        nStyle=wb.createCellStyle();
        font=wb.createFont();

        for(int  i=0;i<title.length;i++){
            nCell=nRow.createCell(i+1);
            nCell.setCellValue(title[i]);
            nCell.setCellStyle(this.Title(wb, nStyle, font));

        }
        //初始化
        nStyle=wb.createCellStyle();
        font=wb.createFont();

        //換行
        for(int j=0;j<dataList.size();j++){
            OutProductVO op=dataList.get(j);
            colNo=1;

            nRow=sheet.createRow(rowNo++);

            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getCustomName());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getProductNo());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getContractNo());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getCnumber());
            nCell.setCellStyle(this.Text(wb, nStyle, font));



            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getFactoryName());
            nCell.setCellStyle(this.Text(wb, nStyle, font));



            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getExts());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getDeliveryPeriod());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getSpipTime());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getTradeTerms());
            nCell.setCellStyle(this.Text(wb, nStyle, font));


        }



        //OutputStream  os=new FileOutputStream(new File("D:\\outProduct.xls"));
        /*wb.write(os); os.flush(); os.close();*/

        //下載
        DownloadUtil dUtil=new DownloadUtil();
        ByteArrayOutputStream os=new ByteArrayOutputStream();
        wb.write(os);
        dUtil.download(os, response, "出貨表.xls");


    }

    //大標題的樣式
    public CellStyle bigTitle(Workbook wb,CellStyle nStyle,Font font){
        font.setFontName("宋體");
        font.setFontHeightInPoints((short) 16); 
        //字體加粗
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //橫向居中
        nStyle.setAlignment(CellStyle.ALIGN_CENTER);
        //縱向居中
        nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //單元格垂直居中

        nStyle.setFont(font);
        return nStyle;
    }

    //標題樣式
    public CellStyle Title(Workbook wb,CellStyle nStyle,Font font){
        font.setFontName("黑體");
        font.setFontHeightInPoints((short) 12); 

        //橫向居中
        nStyle.setAlignment(CellStyle.ALIGN_CENTER);
        //縱向居中
        nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //單元格垂直居中

        //表格線
        nStyle.setBorderTop(CellStyle.BORDER_THICK); //粗實線
        nStyle.setBorderBottom(CellStyle.BORDER_THIN); //實線
        nStyle.setBorderLeft(CellStyle.BORDER_THIN); //比較粗實線
        nStyle.setBorderRight(CellStyle.BORDER_THIN); //實線

        nStyle.setFont(font);
        return nStyle;
    }

    //文字樣式
    public CellStyle Text(Workbook wb,CellStyle nStyle,Font font){
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short) 10); 

        //橫向居中
        nStyle.setAlignment(CellStyle.ALIGN_CENTER);
        //縱向居中
        nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //單元格垂直居中

        //表格線

        nStyle.setBorderBottom(CellStyle.BORDER_THIN); //實線
        nStyle.setBorderLeft(CellStyle.BORDER_THIN); //比較粗實線
        nStyle.setBorderRight(CellStyle.BORDER_THIN); //實線

        nStyle.setFont(font);
        return nStyle;
    }

方案2

前面提到的方法是要一個個的設置樣式,感受仍是比較複雜的,那麼若是咱們提早作好一個模板,而後把經過代碼看來讀取模板的excel文件中的樣式,而後按照這種模板的格式再去填充數據的話,效果應該會更好一點的噢!

一、首先要獲取模板文件的路徑,就是你把這個文件放在哪裏了。

//獲取模板存放的路徑
        String path=request.getSession().getServletContext().getRealPath("/")+"/make/xlsprint/";
        InputStream  is=new FileInputStream(new File(path+"出貨表.xls"));

二、獲取模板上的單元格樣式

nRow=sheet.getRow(2);

三、獲取須要的樣式。getCell()指的就是從第幾列獲取。咱們是從0開始計算的。

 //客戶的樣式 nCell=nRow.getCell(1); CellStyle customStyle=nCell.getCellStyle(); //訂單的樣式 nCell=nRow.getCell(2); CellStyle contractNoStyle=nCell.getCellStyle(); //貨號的樣式 nCell=nRow.getCell(3); CellStyle productNoStyle=nCell.getCellStyle(); //數量的樣式 nCell=nRow.getCell(4); CellStyle numStyle=nCell.getCellStyle(); //生產廠家的樣式 nCell=nRow.getCell(5); CellStyle factoryStyle=nCell.getCellStyle(); //日期的樣式 nCell=nRow.getCell(6); CellStyle dateStyle=nCell.getCellStyle(); //貿易條款 nCell=nRow.getCell(8); CellStyle tradeStyle=nCell.getCellStyle();

四、把樣式設置到表格的值中。

nCell.setCellStyle(customStyle);

完整代碼以下:

//方案2
    //模板
    @RequestMapping("/cargo/outproduct/printTemple.action")
    public void printTemple(String inputDate,HttpServletRequest request,HttpServletResponse  response) throws IOException{
        List<OutProductVO> dataList = outProductService.find(inputDate);

        //獲取模板存放的路徑
        String path=request.getSession().getServletContext().getRealPath("/")+"/make/xlsprint/";
        InputStream  is=new FileInputStream(new File(path+"出貨表.xls"));


        Workbook wb=new HSSFWorkbook(is);
        Sheet sheet = wb.getSheetAt(0);
        Row nRow=null;
        Cell nCell=null;

        //行號
        int  rowNo=0;
        //列號
        int colNo=1;

        //獲取模板上的單元格樣式

        nRow=sheet.getRow(2);

        //客戶的樣式
        nCell=nRow.getCell(1);
        CellStyle customStyle=nCell.getCellStyle();
        //訂單的樣式
        nCell=nRow.getCell(2);
        CellStyle contractNoStyle=nCell.getCellStyle();
        //貨號的樣式
        nCell=nRow.getCell(3);
        CellStyle productNoStyle=nCell.getCellStyle();
        //數量的樣式
        nCell=nRow.getCell(4);
        CellStyle numStyle=nCell.getCellStyle();
        //生產廠家的樣式
        nCell=nRow.getCell(5);
        CellStyle factoryStyle=nCell.getCellStyle();
        //日期的樣式
        nCell=nRow.getCell(6);
        CellStyle dateStyle=nCell.getCellStyle();
        //貿易條款
        nCell=nRow.getCell(8);
        CellStyle tradeStyle=nCell.getCellStyle();


        //大標題
        nRow=sheet.getRow(rowNo++); //獲取一個行對象
        nCell=nRow.getCell(colNo); //獲取一個單元格對象
        nCell.setCellValue(inputDate.replace("-0", "年").replaceFirst("-", "年")+"月份出貨表");

        //跳過靜態表格頭
        rowNo++;


        //換行
        for(int j=0;j<dataList.size();j++){
            OutProductVO op=dataList.get(j);
            colNo=1;

            nRow=sheet.createRow(rowNo++);

            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getCustomName());
            nCell.setCellStyle(customStyle);


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getContractNo());
            nCell.setCellStyle(contractNoStyle);


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getProductNo());
            nCell.setCellStyle(productNoStyle);


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getCnumber());
            nCell.setCellStyle(numStyle);



            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getFactoryName());
            nCell.setCellStyle(factoryStyle);



            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getDeliveryPeriod());
            nCell.setCellStyle(dateStyle);


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getSpipTime());
            nCell.setCellStyle(dateStyle);


            nCell=nRow.createCell(colNo++);
            nCell.setCellValue(op.getTradeTerms());
            nCell.setCellStyle(tradeStyle);
        }

        //下載
        DownloadUtil dUtil=new DownloadUtil();
        ByteArrayOutputStream os=new ByteArrayOutputStream();
        wb.write(os);
        dUtil.download(os, response, "出貨表.xls");
        os.flush();
        os.close();

    }

方案3

嗯,好吧,你可能以爲上面這個模板方式還挺好的,好吧,那咱們再來作一個優化!我在開篇就提到了,有的朋友導出數據3-4萬的時候系統就撐不下去了,因此咱們對於百萬級的數據導出還須要進行優化。其實很是簡單。
咱們前面導出的數據是一個xls的文件,熟悉word的朋友都知道,office1997-2003版本的excel是xls版本的。咱們一樣還有2007及其以上的版本,是xlsx格式的後綴文件。因此咱們能夠把文件導出爲xlsx的文件。

修改:

Workbook wb=new HSSFWorkbook(is);
        Sheet sheet = wb.getSheetAt(0);

Workbook wb=new XSSFWorkbook(is);  //2007版本
            Sheet sheet = wb.getSheetAt(0);

就能夠了,是否是很是簡單,哈哈,固然咯,你的這個模板文件也須要換成xlsx的文件噢,導出文件也須要換成xlsx格式的。

四、excel數據批量導入

若是有須要的朋友,也能夠嘗試一下批量導入功能。

//導入
    @RequestMapping("/basicinfo/factory/importInfo.action")
    public String importInfo() throws InvalidFormatException, IOException{
        /* * 開發步驟: * 一、讀取xls文件;建立模板,設置全部單元格爲文本類型,而後黏貼數據到editplus,再黏貼到excel中,這樣就不怕讀取類型錯誤。 * 二、拼接成sql * 三、批量插入 */

        //實現自動識別讀取的xls版本,建立其對應的對象; 2003 HSSFWorkbook對象;2007 XSSFWorkbook對象
        String sql = "";
        File file = new File("c:\\7F28E200.xlsx");
        Workbook wb = new WorkbookFactory().create(file); //自動識別excel版本
        Sheet sheet = wb.getSheetAt(0);

        Row nRow = sheet.getRow(1);
        Cell nCell = null;

        //是否標題行,標題行還能夠多列
        int beginRowNo = 0; //開始行
        int endRowNo =  sheet.getLastRowNum(); //結束行
        int beginColNo = 0; //開始列
        int endColNo = nRow.getLastCellNum(); //結束列

        StringBuffer sBuf = new StringBuffer();
        for(int i=beginRowNo;i<endRowNo;i++){
            nRow = sheet.getRow(i); //行對象
    //          
    //          for(int j=beginColNo;j<endColNo;j++){
    //              nCell = nRow.getCell(j++); //單元格
    //              System.out.println(nCell.toString());
    //          }

            beginColNo = 0; //開始列
`// `       

            sBuf.append("insert into factory_c (FACTORY_ID,FULL_NAME,FACTORY_NAME,CONTACTS,PHONE,MOBILE,FAX,INSPECTOR,CNOTE,ORDER_NO,STATE,CREATE_BY,CREATE_DEPT,CREATE_TIME) ");
            sBuf.append("values(");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            //orderNo
            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("null");
            }else{
                sBuf.append(UtilFuns.convertNull(nCell.getStringCellValue()));
            }
            sBuf.append(",");

            //state
            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("null");
            }else{
                sBuf.append(UtilFuns.convertNull(nCell.getStringCellValue()));
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("''");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }
            sBuf.append(",");

            nCell = nRow.getCell(beginColNo++); 
            if(nCell==null){
                sBuf.append("NULL");
            }else{
                sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
            }

            sBuf.append(");");

        }
        for(String s : sBuf.toString().split(";")){
            System.out.println(s);
        }

        sqlDao.batchSQL(sBuf.toString().split(";")); //執行insert sql

        return "redirect:/basicinfo/factory/list.action";
    }

關於POI的文件下載你也能夠參考這篇文章:http://blog.csdn.net/article/detailssdksdk0//52557755

源碼下載:https://github.com/sdksdk0/JK

關於poi的一些基本操做方法和api文檔下載地址:http://download.csdn.net/detail/sdksdk0/9696976

相關文章
相關標籤/搜索