Java生成Excel

public class SimpleExcelWrite {
 public void createExcel(OutputStream os) throws WriteException,IOException{
//  创建工作薄
  WritableWorkbook workbook=Workbook.createWorkbook(os);
//  创建新的一页
  WritableSheet sheet=workbook.createSheet("First Sheet", 0);
//  创建要显示的具体内容
  Label xuexiao=new Label(0,0,"学校");
  sheet.addCell(xuexiao);
  Label zhuanye=new Label(1,0,"专业");
  sheet.addCell(zhuanye);
  Label jingzhengli=new Label(2,0,"竞争力");
  sheet.addCell(jingzhengli);
  
  Label qinghua=new Label(0,1,"清华大学");
  sheet.addCell(qinghua);
  Label jisuanji=new Label(1,1,"计算机专业");
  sheet.addCell(jisuanji);
  Label gao=new Label(2,1,"高");
  sheet.addCell(gao);
  
  Label beida=new Label(0,2,"北京大学");
  sheet.addCell(beida);
  Label falv=new Label(1,2,"法律专业");
  sheet.addCell(falv);
  Label zhong=new Label(2,2,"中");
  sheet.addCell(zhong);
  
  Label ligong=new Label(0,3,"北京理工大学");
  sheet.addCell(ligong);
  Label hangkong=new Label(1,3,"航空专业");
  sheet.addCell(hangkong);
  Label di=new Label(2,3,"低");
  sheet.addCell(di);
  
//  把创建的内容写入到输出流中,并关闭输出流
  workbook.write();
  workbook.close();
  os.close();
  
 }
}

 

<%
 String fname="学校专业竞争力情况";
 OutputStream os=response.getOutputStream();
 response.reset();
 
 //下面是对中文文件名的处理
 response.setCharacterEncoding("UTF-8");//设置相应内容的编码格式
 fname=java.net.URLEncoder.encode(fname,"UTF-8");
 response.setHeader("Content-Disposition","attachment;filename="+ new String(fname.getBytes("UTF-8"),"GBK")+".xls");
 response.setContentType("application/msexel");//定义输出类型
 SimpleExcelWrite sw=new SimpleExcelWrite();
 sw.createExcel(os);
 %>

 

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class SimpleExcelView extends AbstractExcelView {

    /**
     * 生成excel视图,可用excel工具打开或者保存 单行表头,简单模板
     * 例子可参考:问题反馈_excel导出
     */
    public void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook,
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 设置文件名
        // 创建sheet
        HSSFSheet sheet = workbook.createSheet();
        
        // 设置样式
        HSSFCellStyle hssCellStyle = getGeneralCellStyle(workbook);

        // 处理表格头
        String[] headArr = (String[]) model.get("head");
        sheet.setDefaultColumnWidth(headArr.length);
        HSSFCell cell = null;
        int colNum = headArr.length;
        for (int i = 0; i < colNum; i++) {
            cell = getCell(sheet, 0, i);
            setText(cell, headArr[i]);
            cell.setCellStyle(hssCellStyle);
            // 设置列宽
            sheet.setColumnWidth(i, 5000);
        }

        // 处理数据
        List<List<Object>> data = (List<List<Object>>) model.get("data");
        List<Object> row = null;
        int rowNum = data.size();
        for (int i = 0; i < rowNum; i++) {
            row = data.get(i);
            for (int j = 0; j < colNum; j++) {
                cell = getCell(sheet, i + 1, j);
                setText(cell, "" + row.get(j));
                cell.setCellStyle(hssCellStyle);
            }
        }

    }

    /**
     * 设置样式
     * 
     * @param cell
     * @param workbook
     */
    public HSSFCellStyle getGeneralCellStyle(HSSFWorkbook workbook) {
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setLocked(false);

        // 设置Excel中的边框
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setTopBorderColor(HSSFColor.BLACK.index);

        return cellStyle;
    }
}

 

 

posted @ 2011-11-24 21:41  残星  阅读(699)  评论(0编辑  收藏  举报