先寫入一行作為表格頭部,然后查詢?nèi)〉靡粋€(gè)list,然后循環(huán)并調(diào)用createCell方法放入excel表中:

          public void CreateExcel(String filename){
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("new sheet");
            HSSFRow row = sheet.createRow((short) 0);
            createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER,"Login Name");
            createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION,"Login Name");
            createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL,"Password");
            createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL,"Name");
            createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY,"Create Time");
            
            List ls = manager.getAllUserExcel();
           
            int i = 1;
            for (Iterator it = ls.iterator();it.hasNext();i++){
            row = sheet.createRow((short) i);
            Object[] o = (Object[])it.next();
            
            String a="";
            try{
            a = o[0].toString();
            }catch(Exception e){};
            String b="";
            try{
            b = o[1].toString();
            }catch(Exception e){};
            String c="";
            try{
            c = o[2].toString();
            }catch(Exception e){};
            String d="";
            try{
            d = o[3].toString();
            }catch(Exception e){};
            String e="";
            try{
            e = o[4].toString();
            }catch(Exception z){};  
            
            createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER,a);
            createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION,b);
            createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL,c);
            createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL,d);
            createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY,e); 
            
            } 
            
            // Write the output to a file
            FileOutputStream fileOut = null;
            try {
             fileOut = new FileOutputStream(filename);
            } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            try {
             wb.write(fileOut);
            } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            try {
             fileOut.close();
            } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
           }

          createCell方法:
          private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align,String value)
           {
            HSSFCell cell = row.createCell(column);
            cell.setCellValue(value);
            HSSFCellStyle cellStyle = wb.createCellStyle();
            cellStyle.setAlignment(align);
            cell.setCellStyle(cellStyle);
           }

          用到的包:

          import org.apache.poi.hssf.usermodel.HSSFCell;
          import org.apache.poi.hssf.usermodel.HSSFCellStyle;
          import org.apache.poi.hssf.usermodel.HSSFRichTextString;
          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.hssf.util.HSSFColor;
          posted on 2008-11-17 11:32 lzj520 閱讀(261) 評論(0)  編輯  收藏 所屬分類: 個(gè)人學(xué)習(xí)日記Report
          主站蜘蛛池模板: 长治县| 贵南县| 海宁市| 北京市| 永泰县| 米林县| 鲁甸县| 股票| 宁河县| 襄樊市| 宁南县| 太仆寺旗| 虹口区| 南城县| 宁都县| 宿州市| 万载县| 齐齐哈尔市| 铜鼓县| 内乡县| 榆社县| 思南县| 兴山县| 高雄县| 云安县| 凤台县| 柘城县| 双辽市| 疏附县| 赣州市| 扶绥县| 澎湖县| 莱阳市| 陆河县| 邯郸县| 鄂托克旗| 禹州市| 登封市| 无棣县| 乐安县| 祁阳县|