Workbook

package tf56.goodstaxiAdmin.util;apache

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.json.JSONArray;
import org.json.JSONObject;json

/**
 * excel 生成工具
 * @author lenovo
 *
 */
public class ExcelUtils {工具

    /**
     * 
     * @param data  數據
     * @param header 標題
     * @param itemnames  標題對應的數據字段名
     * @return
     */
    public static Workbook createWorkbook(JSONArray data,String[] header,String []itemnames,String sheetname){
         Workbook workbook = null;
         try {
             // XSSFWork 2007 HSSWorkbook 2003
             workbook = new HSSFWorkbook();
         }catch(Exception e) {
             System.out.println("It cause Error on CREATING excel workbook: ");
             e.printStackTrace();
         }
         if(workbook != null) {
             Sheet sheet = workbook.createSheet(sheetname);
             CellStyle style = getStyle(workbook);
             Row row0 = sheet.createRow(0);
             for(int i = 0; i < header.length; i++) {
                 Cell cell_1 = row0.createCell(i, Cell.CELL_TYPE_STRING);
                 cell_1.setCellStyle(style);
                 cell_1.setCellValue(header[i]);
                 sheet.autoSizeColumn(i);
             }
             if(data == null || data.length() == 0) {
                 Row row = sheet.createRow(1);
                 Cell cell = row.createCell(0, Cell.CELL_TYPE_STRING);
                 cell.setCellValue("沒有數據");
             } else {
                 for (int rowNum = 1; rowNum <= data.length(); rowNum++) {
                     Row row = sheet.createRow(rowNum);
                     JSONObject item = data.getJSONObject(rowNum-1);
                     for(int i=0;i<itemnames.length;i++) {
                         Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
                         String value = "";
                         if(!item.isNull(itemnames[i])) {
                            value = item.get(itemnames[i]) + "";
                         }
                         cell.setCellValue(value);
                     }
                     
                 }
             }字體

         }
         return workbook;
    }
    
     private static CellStyle getStyle(Workbook workbook){
            CellStyle style = workbook.createCellStyle();
            style.setAlignment(CellStyle.ALIGN_CENTER); 
            style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
            // 設置單元格字體
            Font headerFont = workbook.createFont(); // 字體
            headerFont.setFontHeightInPoints((short)14);
            headerFont.setColor(HSSFColor.RED.index);
            headerFont.setFontName("宋體");
            style.setFont(headerFont);
            style.setWrapText(true);this

            // 設置單元格邊框及顏色
            style.setBorderBottom((short)1);
            style.setBorderLeft((short)1);
            style.setBorderRight((short)1);
            style.setBorderTop((short)1);
            style.setWrapText(true);
            return style;
        }
}
 .net

   

 

 

Workbook workbook = ExcelUtils.createWorkbook(data, header, itemnames, "交易列表信息"); excel

FileOutputStream fileOut = null; 
        try {
            /*OutputStream os = response.getOutputStream();
            workbook.write(os);
            response.flushBuffer();*/
            String relativelyPath=this.getResponseHelper().getRequest().getSession().getServletContext().getRealPath(""); 
            File file = new File(relativelyPath+"/temp");
            if(!file.exists()){
                file.mkdir();
            }
             file = new File(relativelyPath+"/temp/trade.xls");
            if(file.exists()){
                file.delete();
            }
            file.createNewFile();
          
            fileOut = new FileOutputStream(file);  
            workbook.write(fileOut);
            this.zip(relativelyPath+"/temp.zip", new File(relativelyPath+"/temp"));ip

相關文章
相關標籤/搜索