`
xp9802
  • 浏览: 1202128 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

POI导出Excel例子

阅读更多

package com.ufida.cutm.export;

 

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

 

import com.ufida.cutm.dao.DBConnection;

import com.ufida.cutm.util.ConstantsExcel;

import com.ufida.cutm.util.ExcelWriter;

import com.ufida.cutm.util.StringUtilCommon;

import com.ufida.cutm.util.ZipCompressorByAnt;

import com.ufida.cutm.vo.T1;

 

 

/**

 * 测试导出excel

 * 最大数据量

 * 2011-3-9 22:00

 * @author zengq

 *

 */

public class ExportExcel {

 

    public static final String[] excelHeader=new String[]{"id","ida","idb","name","pwd","aa","bb","cc","dd","ee","ff","gg",

                                                           "hh","ii","jj","kk","ll","mm","nn","oo","pp","qq","rr","ss","tt"};

     

    public static final String File_Path="E:\\projects\\java\\ws_0\\export\\data";

    /**

     * @author zengq

     * @param sql 获取记录行数的sql

     * @param recordStr sql语句中记录数的别名

     * @return 总记录数

     * 2011-3-10 14:25

     * @throws Exception

     */

    public  Long getRecordNumber(String sql,String recordStr) throws Exception{

        Connection conn=null;

        PreparedStatement pst=null;

        ResultSet rs1=null;

        recordStr=StringUtilCommon.getPropertyValue(recordStr);

        try {

            Long recordNum=0l;

            conn=DBConnection.getConnection();

 

             pst=conn.prepareStatement(sql);

             rs1=pst.executeQuery();

             

            while(rs1.next()){

                if(recordStr.length()==0){

                    recordNum=rs1.getLong(1);

                }else{

                    recordNum=rs1.getLong(recordStr);

                }

            }

            return recordNum;

        } catch (RuntimeException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

            closeResource(rs1,pst,conn);

        }

        return new Long(0);

    }

     

    public List getAll(String sql) throws Exception{

        Connection conn=null;

        PreparedStatement pst=null;

        ResultSet rs1=null;

        List list=null;

        //FileOutputStream out=null;

        FileWriter out=null;

        try {

             list=new ArrayList();

            if(sql==null){

             sql="select *from t1 ";

            }

            conn=DBConnection.getConnection();

//      Statement st=conn.createStatement();

//      ResultSet rs1=st.executeQuery(sql);

            pst=conn.prepareStatement(sql);

            rs1=pst.executeQuery();

            rs1.last();

            System.out.println("rs1.getRow()="+rs1.getRow());

            rs1.beforeFirst();

            File file=new File("c:/test.doc");

//          if(!file.exists()){

//              file=new File("c:/test.doc");

//          }

             //out=new FileOutputStream(file,true);

//           out=new FileWriter(file,true);

            while(rs1.next()){

                T1 t=new T1();

                t.setId(rs1.getLong("id"));

                t.setIda(rs1.getLong("ida"));

                t.setIdb(rs1.getString("idb"));

                t.setName(rs1.getString("name"));

                t.setPwd(rs1.getString("pwd"));

                 

                t.setAa(rs1.getString("aa"));

                t.setBb(rs1.getString("bb"));

                t.setCc(rs1.getString("cc"));

                t.setDd(rs1.getString("dd"));

                 

                t.setEe(rs1.getString("ee"));

                t.setFf(rs1.getString("ff"));

                t.setGg(rs1.getString("gg"));

                t.setHh(rs1.getString("hh"));

                t.setIi(rs1.getString("ii"));

                t.setJj(rs1.getString("jj"));

                t.setKk(rs1.getString("kk"));

                t.setLl(rs1.getString("ll"));

                t.setMm(rs1.getString("mm"));

                t.setNn(rs1.getString("nn"));

                t.setOo(rs1.getString("oo"));

                t.setPp(rs1.getString("pp"));

                t.setQq(rs1.getString("qq"));

                t.setRr(rs1.getString("rr"));

                t.setSs(rs1.getString("ss"));

                t.setTt(rs1.getString("tt"));

                 

                //System.out.println("t1="+t);

                //out.write(t.toString().getBytes());

//              out.write(t.toString()+"\t\r");

                list.add(t);

            }

            System.out.println("空一行*********************************************");

             

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }finally{

            closeResource(rs1,pst,conn);

            if(out!=null){

                out.flush();

                out.close();

                out=null;

            }

        }

        return list;

         

}

 

    public  void closeResource(ResultSet rs1,Statement pst,Connection conn) throws Exception{

        if(rs1!=null){

            rs1.close();

            rs1=null;

        }if(pst!=null){

            pst.close();

            pst=null;

        }

        DBConnection.closeConnection(conn);

        System.gc();

    }

    /**

     * 获取总页数

     * @param recordNums

     * @param pageSize

     * @return

     * @throws Exception

     */

    public static Long getTotalPage(Long recordNums,Long pageSize) throws Exception{

        if(recordNums==0||pageSize==0)

            return 0l;

        return (long)Math.ceil(recordNums/(double)pageSize);

    }

     

    /**

     * 为sql语句增加提取数

     * @param basicSql

     * @param begin

     * @param limitNum

     * @return

     * @throws Exception

     */

    public static String getPageSql(String basicSql,Long begin,Long limitNum) throws Exception{

        basicSql=StringUtilCommon.getPropertyValue(basicSql);

        if(basicSql.length()==0)

            return "";

        String sql=" limit "+begin+","+limitNum;

        return sql;

    }

     

    public Map getPageList(Map argsMap) throws Exception{

        String path=(String)argsMap.get("path");

        if(path==null){

            path=this.File_Path;

        }

        File  file=new File(path+"/test0.xls");

        String sql="select count(*) record from t1";

        Long totalNum=getRecordNumber(sql, "");

        System.out.println("totalNum="+totalNum);

        Long pageSize=10000l;

        Long totalPage=getTotalPage(totalNum, pageSize);

        System.out.println("totalPage="+totalPage);

        String sql2="select *from t1 limit ";

        ExcelWriter writer=new ExcelWriter();

        for (int i = 0; i < totalPage; i++) {

            if(i==0){

                sql2+=" 0,"+pageSize;

                System.out.println("sql2="+sql2);

                List list=getAll(sql2);

                WriteToExcel(file,list);

            }else{

                sql2="select *from t1 limit "+pageSize*i+","+pageSize;

                System.out.println("sql2="+sql2);

                List list=getAll(sql2);

                WriteToExcel(new File(path+"/test"+i+".xls"), list);

                //reWirte(file, list);

            }

             

        }

         

        return null;

    }

    HSSFWorkbook workbook=null;

    HSSFSheet sheet=null;

    HSSFRow row=null;

    HSSFCell cell=null;

    public void WriteToExcel(File file,List list) throws Exception{

        if(file==null&&!file.exists()){

            System.out.println("文件不能为空!");

            return;

        }

         

        FileOutputStream out=new FileOutputStream(file,true);

        BufferedOutputStream buffout=new BufferedOutputStream(out);

        ExcelWriter writer=new ExcelWriter();

        writer=new ExcelWriter(buffout);

        writer.createRow(0);

        for (int i = 0; i < excelHeader.length; i++) {

            writer.setCell(i, excelHeader[i]);

        }

        int rownum=0;

        for (int j=0;j<list.size();j++) {

            T1 t=(T1)list.get(j);

            //System.out.println("WriteToExcel="+t);

            //Thread.sleep(1000);

            writer.createRow(rownum++);

            for(int k=0;k<excelHeader.length;k++){

                String header=excelHeader[k];

                //System.out.println("j="+j+",k="+k+",header="+header);

                if(header.equals("id")){

                  writer.setCell(0,t.getId());

                }else if(header.equals("ida")){

                    writer.setCell(1,t.getIda()+"ida");

                }else if(header.equals("idb")){

                    writer.setCell(2,t.getIdb()+"idb");

                }else if(header.equals("name")){

                    writer.setCell(3,t.getName()+"name");

                }else if(header.equals("pwd")){

                    writer.setCell(4,t.getPwd()+"pwd");

                }

                //下一个10行

                else if(header.equals("aa")){

                    writer.setCell(5,t.getAa()+"aa");

                }else if(header.equals("bb")){

                    writer.setCell(6,t.getBb()+"bb");

                }else if(header.equals("cc")){

                    writer.setCell(7,t.getCc()+"cc");

                }else if(header.equals("dd")){

                    writer.setCell(8,t.getDd()+"dd");

                }else if(header.equals("ee")){

                    writer.setCell(9,t.getEe()+"ee");

                }else if(header.equals("ff")){

                    writer.setCell(10,t.getFf()+"ff");

                }else if(header.equals("gg")){

                    writer.setCell(11,t.getGg()+"gg");

                }else if(header.equals("hh")){

                    writer.setCell(12,t.getHh()+"hh");

                }else if(header.equals("ii")){

                    writer.setCell(13,t.getIi()+"ii");

                }else if(header.equals("jj")){

                    writer.setCell(14,t.getJj()+"jj"); 

                }

                //下一个10行

                else if(header.equals("kk")){

                    writer.setCell(15,t.getKk()+"kk");

                }else if(header.equals("ll")){

                    writer.setCell(16,t.getLl()+"ll");

                }else if(header.equals("mm")){

                    writer.setCell(17,t.getMm()+"mm");

                }else if(header.equals("nn")){

                    writer.setCell(18,t.getNn()+"nn");

                }else if(header.equals("oo")){

                    writer.setCell(19,t.getOo()+"oo");

                }else if(header.equals("pp")){

                    writer.setCell(20,t.getPp()+"pp");

                }else if(header.equals("qq")){

                    writer.setCell(21,t.getQq()+"qq");

                }else if(header.equals("rr")){

                    writer.setCell(22,t.getRr()+"rr");

                }else if(header.equals("ss")){

                    writer.setCell(23,t.getSs()+"ss");

                }else if(header.equals("tt")){

                    writer.setCell(24,t.getTt()+"tt");

                }

                 

            }

        }

         try {

               writer.export();

               writer.releaseResource();

               System.out.println("file.size="+file.length()/(1000*1000)+"mb");

               System.out.println(" 导出Excel:文件[成功] ");

              } catch (IOException ex) {

               System.out.println(" 导出Excel:文件[失败] ");

               ex.printStackTrace();

              }

    }

    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub

            ExportExcel export = new ExportExcel();

 

        try {

            Long start=System.currentTimeMillis();

            export.getPageList(new HashMap());

            Long end=System.currentTimeMillis();

            System.out.println("生成excel總計时间="+(end-start));

            //export.buildExcelByXml(null, null);

//          export.buildMutiSheetExcel(null);

            System.out.println("*************生成excel成功!");

             

        } catch (Exception e) {

            System.out.println("*************生成excel失败!");

            e.printStackTrace();

        }

        try {

            System.out.println("*****************下面开始打包!");

            Long start=System.currentTimeMillis();

            ZipCompressorByAnt zca = new ZipCompressorByAnt("e:/ZipCompressorByAnt.zip");

            zca.compress(export.File_Path);

            Long end=System.currentTimeMillis();

            System.out.println("打包總計时间="+(end-start));

            System.out.println("*****************打包成功!");

        } catch (RuntimeException e) {

            System.out.println("*****************打包失败!");

            e.printStackTrace();

        }

    }

     

     

     

     

     

     

     

     

     

     

     

 

     

     

     

     

     

     

    //追加行

    public void reWirte(File file,List list) throws FileNotFoundException, IOException {

        //获取workbook

        HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(

                new FileInputStream(file)));

        //获取sheet

        HSSFSheet sheet = wb.getSheetAt(0);

        //获取最后一行行号,从0开始

        int maxRow = sheet.getLastRowNum();

        HSSFRow row = null;//sheet.createRow(maxRow + 1);

//      row.createCell((short) 0).setCellValue("追加的1");

     

        int rownum=0;

        for (int j=0;j<list.size();j++) {

            T1 t=(T1)list.get(j);

            //System.out.println("WriteToExcel="+t);

            row=sheet.createRow(++maxRow);

            for(int k=0;k<excelHeader.length;k++){

                String header=excelHeader[k];

                //System.out.println("j="+j+",k="+k+",header="+header);

                if(header.equals("id")){

                  row.createCell((short) 0).setCellValue(t.getId());

                }else if(header.equals("ida")){

                    row.createCell((short) 1).setCellValue(t.getIda());

                }else if(header.equals("idb")){

                    row.createCell((short) 2).setCellValue(t.getIdb()+"idb");

                }else if(header.equals("name")){

                    row.createCell((short) 3).setCellValue(t.getName()+"name");

                }else if(header.equals("pwd")){

                    row.createCell((short) 4).setCellValue(t.getPwd()+"pwd");

                }else if(header.equals("aa")){

                    row.createCell((short) 5).setCellValue(t.getAa()+"aa");

                }else if(header.equals("bb")){

                    row.createCell((short) 6).setCellValue(t.getBb()+"bb");

                }else if(header.equals("cc")){

                    row.createCell((short) 7).setCellValue(t.getCc()+"cc");

                }else if(header.equals("dd")){

                    row.createCell((short) 8).setCellValue(t.getDd()+"dd");

                }else if(header.equals("ee")){

                    row.createCell((short) 9).setCellValue(t.getEe()+"ee");

                }else if(header.equals("ff")){

                    row.createCell((short) 10).setCellValue(t.getFf()+"ff");

                }else if(header.equals("gg")){

                    row.createCell((short) 11).setCellValue(t.getGg()+"gg");

                }else if(header.equals("hh")){

                    row.createCell((short) 12).setCellValue(t.getHh()+"hh");

                }else if(header.equals("ii")){

                    row.createCell((short) 13).setCellValue(t.getIi()+"ii");

                }else if(header.equals("jj")){

                    row.createCell((short) 14).setCellValue(t.getJj()+"jj");   

                }else if(header.equals("kk")){

                    row.createCell((short) 15).setCellValue(t.getKk()+"kk");

                }else if(header.equals("ll")){

                    row.createCell((short) 16).setCellValue(t.getLl()+"ll");

                }else if(header.equals("mm")){

                    row.createCell((short) 17).setCellValue(t.getMm()+"mm");

                }else if(header.equals("nn")){

                    row.createCell((short) 18).setCellValue(t.getNn()+"nn");

                }else if(header.equals("oo")){

                    row.createCell((short) 19).setCellValue(t.getOo()+"oo");

                }else if(header.equals("pp")){

                    row.createCell((short) 20).setCellValue(t.getPp()+"pp");

                }else if(header.equals("qq")){

                    row.createCell((short) 21).setCellValue(t.getQq()+"qq");

                }else if(header.equals("rr")){

                    row.createCell((short) 22).setCellValue(t.getRr()+"rr");

                }else if(header.equals("ss")){

                    row.createCell((short) 23).setCellValue(t.getSs()+"ss");

                }else if(header.equals("tt")){

                    row.createCell((short) 24).setCellValue(t.getTt()+"tt");

                }

                 

            }

        }

        FileOutputStream fos = new FileOutputStream(file);

        try {

            wb.write(fos);

            fos.close();

            System.out.println("*****************追加文件成功!");

        } catch (RuntimeException e) {

            // TODO Auto-generated catch block

            System.out.println("*****************追加文件失败!");

            e.printStackTrace();

        }

    }

     

    /**

     * 通过xml文档生成excel文件

     * @param argsMap

     * @throws Exception

     */

    public void buildExcelByXml(Map<String, String> argsMap,File file) throws Exception{

         

        String sql="select count(*) record from t1";

        Long totalNum=new ExportExcel().getRecordNumber(sql, "");

        System.out.println("totalNum="+totalNum);

        Long pageSize=10000l;

        Long totalPage=ExportExcel.getTotalPage(totalNum, pageSize);

        System.out.println("totalPage="+totalPage);

        String sql2="select *from t1 limit ";

        ExcelWriter writer=new ExcelWriter();

        String contents="";

        for (int i = 0; i < totalPage; i++) {

            if(i==0){

                sql2+=" 0,"+pageSize;

                System.out.println("sql2="+sql2);

                List list=getAll(sql2);

                contents=getXmlExcelContentByList(null, list);

                System.out.println("第"+i+"次*************************contents.length()="+contents.length()/(1000*1000)+"mb");

            }else{

                sql2="select *from t1 limit "+pageSize*i+","+pageSize;

                System.out.println("sql2="+sql2);

                List list=getAll(sql2);

                contents+=getXmlExcelContentByList(null, list);

                System.out.println("第"+i+"次**************************contents.length()="+contents.length()/(1000*1000)+"mb");

            }

             

        }

        if(argsMap==null){

          argsMap=new HashMap();

        }

        argsMap.put(ConstantsExcel.Excel_Main_String_Key, contents);

        createXmlExcel(argsMap,file);

    }

     

    public void createXmlExcel(Map map,File file) throws Exception{

        if(file==null){

             file = new File("c:/exprot.xls");

              

        }if(!file.exists()){

            file.mkdir();

        }

         

        StringBuffer bf = new StringBuffer();

        bf.append(ConstantsExcel.Excel_Header_String);

        String contents=(String) map.get(ConstantsExcel.Excel_Main_String_Key);

        if(contents==null||contents.length()==0){

            contents="";

        }

        bf.append(contents);

         

        bf.append(ConstantsExcel.Excel_Foot_String);

        try {

            FileWriter write=new FileWriter(file);

            //write.append(bf.toString());

            write.write(bf.toString());

            write.flush();

            write.close();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

         

    }

     

    public String getXmlExcelContentByList(String str,List list) throws Exception{

        if(str==null){

          str="";

        }

        if(list==null||list.size()==0){

            return null;

        }

        System.out.println("*****************开始生成content!");

        for (Object obj : list) {

            T1 t=(T1) obj;

            str+="<tr height=19 style='height:14.25pt'>                                "    

            +" <td height=19 width=72 style='height:14.25pt;width:54pt'>"+t.getId()+"</td>    "    

            +" <td width=72 style='width:54pt'>"+t.getIda()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getIdb()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getName()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getPwd()+"</td>                             "   

             

            +" <td width=72 style='width:54pt'>"+t.getAa()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getBb()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getCc()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getDd()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getEe()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getFf()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getGg()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getHh()+"</td>                             "  

            +" <td width=72 style='width:54pt'>"+t.getIi()+"</td>                             "  

            +" <td width=72 style='width:54pt'>"+t.getJj()+"</td>                             "    

             

            +" <td width=72 style='width:54pt'>"+t.getKk()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getLl()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getMm()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getNn()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getOo()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getPp()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getQq()+"</td>                             "    

            +" <td width=72 style='width:54pt'>"+t.getRr()+"</td>                             "   

            +" <td width=72 style='width:54pt'>"+t.getSs()+"</td>                             "

            +" <td width=72 style='width:54pt'>"+t.getTt()+"</td>                             "    

            +"</tr>                                                                " ;

        }

        System.out.println("*****************生成content成功!");

        return str;

    }

     

     

    public Map buildMutiSheetExcel(Map argsMap) throws Exception{

         

        String sql="select count(*) record from t1";

        Long totalNum=new ExportExcel().getRecordNumber(sql, "");

        System.out.println("totalNum="+totalNum);

        Long pageSize=10000l;

        Long totalPage=ExportExcel.getTotalPage(totalNum, pageSize);

        System.out.println("totalPage="+totalPage);

        String sql2="select *from t1 limit ";

         

        String excelName="c:/mutisheet2.xls";

        HSSFWorkbook book=new HSSFWorkbook();

        HSSFRow row=null;

        HSSFCell cell=null;

        File file=new File("c:/sheets.xls");

        FileOutputStream out=null;

        BufferedOutputStream buff=null;

        if(argsMap==null){

            argsMap=new HashMap();

            argsMap.put("book", book);

            argsMap.put("excelName", excelName);

        }

        for (int i = 0; i < totalPage; i++) {

            if(i==0){

                sql2+=" 0,"+pageSize;

                System.out.println("sql2="+sql2);

                List list=getAll(sql2);

                HSSFSheet sheet=book.createSheet();

//              argsMap.put("sheet", sheet);

//              argsMap.put("list", list);

//              buildMutiSheet(argsMap);

                int maxRowNum=sheet.getLastRowNum();

                for (int j=0;j<list.size();j++) {

                    T1 t=(T1)list.get(j);

                    //System.out.println("WriteToExcel="+t);

                    row=sheet.createRow(maxRowNum++);

                    int length=excelHeader.length;

                    for(int k=0;k<5;k++){

                        String header=excelHeader[k];

                        //System.out.println("j="+j+",k="+k+",header="+header);

                        cell=row.createCell((short) k);

                        if(header.equals("id")){

                          cell.setCellValue(t.getId());

                        }else if(header.equals("ida")){

                            cell.setCellValue(t.getIda()+"ida");

                        }else if(header.equals("idb")){

                            cell.setCellValue(t.getIdb()+"idb");

                        }else if(header.equals("name")){

                            cell.setCellValue(t.getName()+"name");

                        }else if(header.equals("pwd")){

                            cell.setCellValue(t.getPwd()+"pwd");

                        }

                    }

                }

                out=new FileOutputStream(file,true);

                buff=new BufferedOutputStream(out);;

                book.write(buff);

                buff.flush();

                buff.close();

                buff=null;

                cell=null;

                row=null;

                sheet=null;

            }else{

                sql2="select *from t1 limit "+pageSize*i+","+pageSize;

                System.out.println("sql2="+sql2);

                HSSFSheet sheet2=book.createSheet();

                 

//              argsMap.put("sheet", sheet);

                List list=getAll(sql2);

                int maxRowNum=sheet2.getLastRowNum();

                for (int j=0;j<list.size();j++) {

                    T1 t=(T1)list.get(j);

                    //System.out.println("WriteToExcel="+t);

                    if(sheet2==null){

                        sheet2=book.createSheet();

                    }

                    if(row==null){

                      row=sheet2.createRow(maxRowNum++);

                    }

                    int length=excelHeader.length;

                    for(int k=0;k<5;k++){

                        String header=excelHeader[k];

                        //System.out.println("j="+j+",k="+k+",header="+header);

                        cell=row.createCell((short) k);

                        if(header.equals("id")){

                          cell.setCellValue(t.getId());

                        }else if(header.equals("ida")){

                            cell.setCellValue(t.getIda()+"ida");

                        }else if(header.equals("idb")){

                            cell.setCellValue(t.getIdb()+"idb");

                        }else if(header.equals("name")){

                            cell.setCellValue(t.getName()+"name");

                        }else if(header.equals("pwd")){

                            cell.setCellValue(t.getPwd()+"pwd");

                        }

                    }

                    out=new FileOutputStream(file,true);

                    buff=new BufferedOutputStream(out);;

                    book.write(buff);

                    buff.flush();

                    buff.close();

                    buff=null;

                    cell=null;

                    row=null;

                    sheet2=null;

//              argsMap.put("list", list);

//              buildMutiSheet(argsMap);

                 

            }

             

         }

        }

         

        return null;

    }

     

    public void buildMutiSheet(Map argsMap) throws Exception{

        String excelName=(String) argsMap.get("excelName");

        excelName=StringUtilCommon.getPropertyValue(excelName);

        if(excelName.length()==0){

            excelName="c:/mutisheet2.xls";

        }

        File file=new File(excelName);

        if(file==null&&!file.exists()){

            file=new File(excelName);

        }

         

        HSSFWorkbook book=(HSSFWorkbook) argsMap.get("book");

        if(book==null){

            book=new HSSFWorkbook();

        }

         

        System.out.println("file.size="+file.length()/(1000*1000)+"mb");

        FileOutputStream out=new FileOutputStream(file,true);

        BufferedOutputStream buffout=new BufferedOutputStream(out);

        HSSFSheet sheet=(HSSFSheet) argsMap.get("sheet");

        HSSFRow row=null;

        HSSFCell cell=null;

        for (int i = 0; i < excelHeader.length; i++) {

            if(sheet==null){

             sheet=book.createSheet();

            }

            if(row==null){

             row=sheet.createRow(0);

            }

            cell=row.createCell((short) i);

            cell.setCellValue(excelHeader[i]);

        }

        cell=null;

        List list=(List) argsMap.get("list");

        if(list==null){

            return;

        }

        //int rownum=0;

        int maxRowNum=sheet.getLastRowNum();

        for (int j=0;j<list.size();j++) {

            T1 t=(T1)list.get(j);

            //System.out.println("WriteToExcel="+t);

            row=sheet.createRow(maxRowNum++);

            int length=excelHeader.length;

            for(int k=0;k<5;k++){

                String header=excelHeader[k];

                //System.out.println("j="+j+",k="+k+",header="+header);

                cell=row.createCell((short) k);

                if(header.equals("id")){

                  cell.setCellValue(t.getId());

                }else if(header.equals("ida")){

                    cell.setCellValue(t.getIda()+"ida");

                }else if(header.equals("idb")){

                    cell.setCellValue(t.getIdb()+"idb");

                }else if(header.equals("name")){

                    cell.setCellValue(t.getName()+"name");

                }else if(header.equals("pwd")){

                    cell.setCellValue(t.getPwd()+"pwd");

                }

                 

                /*else if(header.equals("aa")){

                    cell.setCellValue(t.getAa()+"aa");

                }else if(header.equals("bb")){

                    cell.setCellValue(t.getBb()+"bb");

                }else if(header.equals("cc")){

                    cell.setCellValue(t.getCc()+"cc");

                }else if(header.equals("dd")){

                    cell.setCellValue(t.getDd()+"dd");

                }else if(header.equals("ee")){

                    cell.setCellValue(t.getEe()+"ee");

                }else if(header.equals("ff")){

                    cell.setCellValue(t.getFf()+"ff");

                }else if(header.equals("gg")){

                    cell.setCellValue(t.getGg()+"gg");

                }else if(header.equals("hh")){

                    cell.setCellValue(t.getHh()+"hh");

                }else if(header.equals("ii")){

                    cell.setCellValue(t.getIi()+"ii");

                }else if(header.equals("jj")){

                    cell.setCellValue(t.getJj()+"jj"); 

                }

                 

                else if(header.equals("kk")){

                    cell.setCellValue(t.getKk()+"kk");

                }else if(header.equals("ll")){

                    cell.setCellValue(t.getLl()+"ll");

                }else if(header.equals("mm")){

                    cell.setCellValue(t.getMm()+"mm");

                }else if(header.equals("nn")){

                    cell.setCellValue(t.getNn()+"nn");

                }else if(header.equals("oo")){

                    cell.setCellValue(t.getOo()+"oo");

                }else if(header.equals("pp")){

                    cell.setCellValue(t.getPp()+"pp");

                }else if(header.equals("qq")){

                    cell.setCellValue(t.getQq()+"qq");

                }else if(header.equals("rr")){

                    cell.setCellValue(t.getRr()+"rr");

                }else if(header.equals("ss")){

                    cell.setCellValue(t.getSs()+"ss");

                }else if(header.equals("tt")){

                    cell.setCellValue(t.getTt()+"tt");

                }*/

                cell=null;

            }

        }

         try {

               book.write(buffout);

               buffout.flush();

               buffout.close();

               buffout=null;

               list=null;

               sheet=null;

               row=null;

               cell=null;

               System.out.println(" 导出Excel:文件[成功] ");

              } catch (IOException ex) {

               System.out.println(" 导出Excel:文件[失败] ");

               ex.printStackTrace();

              }

    }

     

    public void writeContent(ExcelWriter writer,List list) throws Exception{

        int rownum=0;

        for (int j=0;j<list.size();j++) {

            T1 t=(T1)list.get(j);

            //System.out.println("WriteToExcel="+t);

            writer.createRow(rownum++);

            for(int k=0;k<excelHeader.length;k++){

                String header=excelHeader[k];

                //System.out.println("j="+j+",k="+k+",header="+header);

                if(header.equals("id")){

                  writer.setCell(0,t.getId());

                }else if(header.equals("ida")){

                    writer.setCell(1,t.getIda()+"ida");

                }else if(header.equals("idb")){

                    writer.setCell(2,t.getIdb()+"idb");

                }else if(header.equals("name")){

                    writer.setCell(3,t.getName()+"name");

                }else if(header.equals("pwd")){

                    writer.setCell(4,t.getPwd()+"pwd");

                }else if(header.equals("aa")){

                    writer.setCell(5,t.getAa()+"aa");

                }else if(header.equals("bb")){

                    writer.setCell(6,t.getBb()+"bb");

                }else if(header.equals("cc")){

                    writer.setCell(7,t.getCc()+"cc");

                }else if(header.equals("dd")){

                    writer.setCell(8,t.getDd()+"dd");

                }

                else if(header.equals("ee")){

                    writer.setCell(9,t.getEe()+"ee");

                }else if(header.equals("ff")){

                    writer.setCell(10,t.getFf()+"ff");

                }

                else if(header.equals("gg")){

                    writer.setCell(11,t.getGg()+"gg");

                }else if(header.equals("hh")){

                    writer.setCell(12,t.getHh()+"hh");

                }

                else if(header.equals("ii")){

                    writer.setCell(13,t.getIi()+"ii");

                }

                    else if(header.equals("jj")){

                    writer.setCell(14,t.getJj()+"jj"); 

                }else if(header.equals("kk")){

                    writer.setCell(15,t.getKk()+"kk");

                }else if(header.equals("ll")){

                    writer.setCell(16,t.getLl()+"ll");

                }else if(header.equals("mm")){

                    writer.setCell(17,t.getMm()+"mm");

                }else if(header.equals("nn")){

                    writer.setCell(18,t.getNn()+"nn");

                }else if(header.equals("oo")){

                    writer.setCell(19,t.getOo()+"oo");

                }else if(header.equals("pp")){

                    writer.setCell(20,t.getPp()+"pp");

                }else if(header.equals("qq")){

                    writer.setCell(21,t.getQq()+"qq");

                }else if(header.equals("rr")){

                    writer.setCell(22,t.getRr()+"rr");

                }else if(header.equals("ss")){

                    writer.setCell(23,t.getSs()+"ss");

                }else if(header.equals("tt")){

                    writer.setCell(24,t.getTt()+"tt");

                }

                 

            }

        }

    }

     

}

分享到:
评论

相关推荐

    1300张图片训练效果

    1300张图片训练效果

    springboot116基于java的教学辅助平台.zip

    教学辅助平台的出现,是为了更好地服务于教育工作者和学生,提高教学效果和学习效率。该平台集成了多个功能模块,旨在为用户提供全面、便捷的教学辅助服务。 平台首页作为导航入口,提供了清晰的界面布局和便捷的导航功能,方便用户快速找到所需功能。需要注意的是,“首页”这一选项在导航菜单中出现了多次,可能是设计上的冗余,需要进一步优化。 “个人中心”模块允许用户查看和管理自己的个人信息,包括修改密码等账户安全设置,确保用户信息的准确性和安全性。 在教育教学方面,“学生管理”和“教师管理”模块分别用于管理学生和教师的信息,包括学生档案、教师资料等,方便教育工作者进行学生管理和教学安排。同时,“课程信息管理”、“科目分类管理”和“班级分类管理”模块提供了课程信息的发布、科目和班级的分类管理等功能,有助于教育工作者更好地组织和管理教学内容。 此外,“课程作业管理”模块支持教师布置和批改作业,学生可以查看和提交作业,实现了作业管理的线上化,提高了教学效率。而“交流论坛”模块则为学生和教师提供了一个交流和讨论的平台,有助于促进师生互动和学术交流。 最后,“系统管理”模块为平台管理员提供了系统配置.

    yolo算法-火灾探测数据集-3466张图像带标签-火灾fire_detect-oqlpv.zip

    yolo系列算法目标检测数据集,包含标签,可以直接训练模型和验证测试,数据集已经划分好,适用yolov5,yolov8,yolov9,yolov7,yolov10,yolo11算法; 包含两种标签格:yolo格式(txt文件)和voc格式(xml文件),分别保存在两个文件夹中; yolo格式:<class> <x_center> <y_center> <width> <height>, 其中: <class> 是目标的类别索引(从0开始)。 <x_center> 和 <y_center> 是目标框中心点的x和y坐标,这些坐标是相对于图像宽度和高度的比例值,范围在0到1之间。 <width> 和 <height> 是目标框的宽度和高度,也是相对于图像宽度和高度的比例值

    基于go语言的参数解析校验器项目资源.zip

    基于go语言的参数解析校验器项目资源

    matlab主成分分析代码

    matlab主成分分析代码

    华南农业大学在四川2020-2024各专业最低录取分数及位次表.pdf

    那些年,与你同分同位次的同学都去了哪里?全国各大学在四川2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据

    Spire.XLS是一个基于.NET的组件

    Spire.XLS是一个基于.NET的组件,使用它我们可以创建Excel文件,编辑已有的Excel并且可以转换Excel文件.dll

    基于爬虫技术的股票分析系统.doc

    现如今,随着互联网的发展,人们获取信息的方式也各有不同。以前的传统方式的信息流与电视,报纸,书籍,信件,等等,因为互联网的使用,现在的互联网媒体已经成为人们获取信息的最重要来源。更新互联网,让人们得到最新、最完整的信息变得越来越容易。 现在企业已经越来越重视互联网所能带来的利益,借助互联网来对自己的企业进行营销推广已经获得绝大部分企业的认可。本文我们主要进行的是股票分析系统网站的设计。何为股票分析,就是指股票投资人之间的根据市场价格对已发行上市的股票进行的买卖。而国内股票市场的迅速发展让这次开发设计显得十分必要。通过该股票分析系统网站,我们可以随时随地通过该股票分析网站,了解股票行业最新信息;根据股票行业分析来进行相关交易。本网站采用的是Springboot技术和mongodb数据库,运用 stock、 vue2、echarts、bootstrap等技术,使用eclipse开发工具完成股票数据的爬取分析。

    厨房食品佐料检测数据集VOC+YOLO格式602张18类别.zip

    文件太大放服务器了,请先到资源详情查看然后下载 样本图参考:blog.csdn.net/2403_88102872/article/details/143395913 数据集格式:Pascal VOC格式+YOLO格式(不包含分割路径的txt文件,仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数):602 标注数量(xml文件个数):602 标注数量(txt文件个数):602 标注类别数:18 标注类别名称:["apple","chocolate","cloth","cononut_water","detergent","fanta","gelatin","kuat","mustard","nescau","peanut","pear","sauce","shoyo","sponge","tangerine","tea","treloso"] 18种常见的厨房食品和佐料,包括苹果、巧克力、椰子水、洗涤剂、饮料、明胶、芥末、花生、酱油等

    基于卷积神经网络参数优化的情感分析论文code_cnn-text-classification.zip

    基于卷积神经网络参数优化的情感分析论文code_cnn-text-classification

    河北传媒学院在四川2020-2024各专业最低录取分数及位次表.pdf

    那些年,与你同分同位次的同学都去了哪里?全国各大学在四川2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据

    Python实现的人脸识别系统及其应用

    内容概要:本文档详细描述了一个基于 Python 的人脸识别系统的构建过程,涵盖了从系统设计理念到具体功能实现的各个方面。首先介绍了系统总体设计流程,包括摄像头图像捕获、人脸检测、特征值计算、特征均值处理以及分类识别。接着深入探讨了 Dlib、NumPy、OpenCV 等关键技术库的应用,特别是 Dlib 人脸检测器接口、人脸预测器接口和人脸识别模型的具体使用方法。最后,本文档介绍了如何通过 Euclidean 距离进行人脸特征比对,实现人脸的成功识别与身份确认。此外,还讨论了人脸识别在实际生活中的多种应用场景和重要意义。 适用人群:具有一定编程基础的软件开发者和技术爱好者,尤其是从事机器学习、图像处理和计算机视觉领域的专业技术人员。 使用场景及目标:①开发人脸识别系统,实现实时图像处理和人脸特征提取;②掌握 Dlib、NumPy、OpenCV 等技术库的实际应用技巧;③深入了解人脸识别技术在安全监控、身份认证、智慧社区等领域的应用。 其他说明:本文档提供了丰富的理论背景和技术实现细节,帮助读者更好地理解和应用人脸识别技术。此外,还包括了一些实用的编码技巧和最佳实践,有助于提高开发效率和代码质量。

    轻量级高性能GO语言开发框架。支持MVC、依赖注入、动态返回.zip

    轻量级高性能GO语言开发框架。支持MVC、依赖注入、动态返回

    stm32的串口hex文件发送与文本文件发送

    stm32的串口hex文件发送与文本文件发送

    广西医科大学在四川2020-2024各专业最低录取分数及位次表.pdf

    那些年,与你同分同位次的同学都去了哪里?全国各大学在四川2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据

    macOS_Sonoma_14.1.1.rdr.split.003

    macOS_Sonoma_14.1.1.rdr.split.003

    天津财经大学珠江学院在四川2020-2024各专业最低录取分数及位次表.pdf

    那些年,与你同分同位次的同学都去了哪里?全国各大学在四川2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据

    400699526844862小爱同学.apk

    400699526844862小爱同学.apk

    微信小程序在校内服务平台的应用及开发

    内容概要:本文介绍了基于微信小程序的校园一体化服务平台的设计与开发。该平台利用微信小程序的便捷性和广泛的用户基础,结合JSP技术和MySQL数据库,实现了个人中心、用户管理、寻物启事管理、物品分类管理、失物招领管理、表白广场管理、吐槽大会管理、二手交易管理、交易类型管理、拼车出行管理和系统管理等多项功能。整个系统具有操作简单、易维护、灵活实用等特点。 适合人群:具有一定编程基础的学生和教师,以及希望深入了解微信小程序开发的技术人员。 使用场景及目标:主要用于高校内的信息管理,如失物招领、物品分类、二手交易等,提升校园生活的便捷性和效率,改善用户体验。 其他说明:系统开发过程中,重点考虑了技术可行性、经济可行性和操作可行性,并进行了详细的系统测试,确保系统的稳定性和可靠性。

    Java课程设计之销售管理系统

    (1)课程设计项目简单描述 鉴于当今超市产品种类繁多,光靠人手动的登记已经不能满足一般商家的需求。我们编辑该程序帮助商家完成产品、商家信息的管理,包括产品、客户、供应商等相关信息的添加、修改、删除等功能。 (2)需求分析(或是任务分析) 1)产品类别信息管理:对客户的基本信息进行添加、修改和删除。 2)产品信息管理:对产品的基本信息进行添加、修改和删除。 3)供应商信息管理: 对供应商的基本信息进行添加、修改和删除。 4)订单信息管理:对订单的基本信 息进行添加、修改和删除。 5)统计报表:按选择日期期间,并按产品类别分组统 计订单金额,使用表格显示统计结果

Global site tag (gtag.js) - Google Analytics