POI

Poi是不错的java操作excel的包。

import java.io.OutputStream;

import java.util.List;

 

import javax.servlet.http.HttpServletResponse;

 

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

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

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

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

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

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

 

public class ExcelAction {

 

    /**

     * 生成excel的通用模版

     *

     * @param response  响应,设置生成的文件类型,文件头编码方式和文件名,以及输出

     * @param list      表的内容,List类型,里面的每个节点是String[]型

     * @param firstLine 标题字符串数组 String[]

     * @param sheetName 表名

     * @param fileName  文件名

     */

    public static void excel(HttpServletResponse response, List list,

           String[] firstLine, String sheetName, String fileName) {

       String[] array1 = null;

       try {

           short i = 0;// row行标

           response.setContentType("application/vnd.ms-excel");//设置生成的文件类型

           response.setHeader("Content-Disposition", "filename="

                  + new String(fileName.getBytes("gb2312"), "iso8859-1"));//设置文件头编码方式和文件名

          

           HSSFWorkbook wb = new HSSFWorkbook();//excel文件,一个excel文件包含多个表

           HSSFSheet sheet = wb.createSheet();//表,一个表包含多个行

           wb.setSheetName(0, sheetName, HSSFWorkbook.ENCODING_UTF_16);// 设置sheet中文编码;

          

           //设置字体等样式

           HSSFFont font = wb.createFont();

           font.setFontHeightInPoints((short) 12);

           font.setFontName("Courier New");

           HSSFCellStyle style = wb.createCellStyle();

           style.setFont(font);

           style.setWrapText(true);

           style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

           style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

          

           HSSFRow row;//行,一行包括多个单元格

           HSSFCell cell;//单元格

           row = sheet.createRow(i);//由HSSFSheet生成行

           row.setHeightInPoints((float) 30);

          

           //生成首行

           for (short j = 0; j < firstLine.length; j++) {

              cell = row.createCell(j);//由行生成单元格

              cell.setCellStyle(style);

              cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;

              cell.setCellValue(firstLine[j]);

              sheet.setColumnWidth(j, (short) (5000));

           }

          

           //生成所有行的单元格内容,如果测试list设为null即可,或者将这一段代码注释掉

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

              // do nothing

           } else {

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

                  row = sheet.createRow(++i);

                  row.setHeightInPoints((float) 33);

                  array1 = (String[]) list.get(k);

                  if (null != array1 && array1.length != 0) {

                     for (int f = 0; f < array1.length; f++) {

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

                         cell.setCellStyle(style);

                         cell.setEncoding(HSSFCell.ENCODING_UTF_16);

                         cell.setCellValue(array1[f]);

                     }

                  }

              }

           }

          

           //输出

           OutputStream out = response.getOutputStream();

           wb.write(out);

           out.close();

       } catch (Exception ex) {

           ex.printStackTrace();

       }

       return;

    }

}

 

 

你可能感兴趣的:(apache,单元测试,Excel,F#,J#)