poi 方式导出excel

 

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

 response.addHeader("content-type", "application/shlnd.ms-excel;charset=utf-8");
 response.addHeader("content-disposition", "attachment; filename="+Excels.toUtf8String("中文文件名称")+".xls"); //文件名
//文件名称若是需要显示成中文,则需要将其转换成UTF-8的方式

   Workbook workBook = Excels.createWorkbook(Excels.Excel_Type.EXCEL_97_2003);
   Sheet sheet = workBook.createSheet("sheet显示的名称");//sheet名
   
//设置标题
   List<String> tableHeadList = new ArrayList<String>();
   tableHeadList.add("单位名称");
   for (int i = 0; i < 10; i++)
   {
    tableHeadList.add("名称"+i);
   }
   Excels.createHeader_(workBook, tableHeadList, sheet, "标题名称 ", tableHeadList.size() - 1);
//设置内容
   List<String> contentList = new ArrayList<String>();
   for (int i = 0; i <10; i++)
   {    
    contentList.add("A"+i);
    Excels.createExcel(workBook, contentList, sheet, i + 2);
   }
   workBook.write(response.getOutputStream());
   response.getOutputStream().close();
---------------------------------------------------------------------------------------------------------------------------------
//excels.java 文件
public static String toUtf8String(String s){
      StringBuffer sb = new StringBuffer();
        for (int i=0;i<s.length();i++){
           char c = s.charAt(i);
           if (c >= 0 && c <= 255){sb.append(c);}
         else{
         byte[] b;
          try { b = Character.toString(c).getBytes("utf-8");}
          catch (Exception ex) {
              System.out.println(ex);
                   b = new byte[0];
          }
             for (int j = 0; j < b.length; j++) {
              int k = b[j];
               if (k < 0) k += 256;
               sb.append("%" + Integer.toHexString(k).toUpperCase());
               }
      }
   }
   return sb.toString();
 }

public static void createHeader_(Workbook workbook,List<String> tableHeadList,Sheet sheet,String titleContent, int headCount)
 {
  //标题样式
  Font titlefont = workbook.createFont();
  titlefont.setFontHeightInPoints((short)12);
  titlefont.setBoldweight(Font.BOLDWEIGHT_BOLD);
  CellStyle title_style = workbook.createCellStyle();
  title_style.setFont(titlefont); 
  title_style.setAlignment(CellStyle.ALIGN_CENTER);
  //列头样式
  Font font = workbook.createFont();
  font.setBoldweight(Font.BOLDWEIGHT_BOLD);
  CellStyle head_style = workbook.createCellStyle();
  head_style.setFont(font); 
  head_style.setAlignment(CellStyle.ALIGN_CENTER);
  CreationHelper createHelper = workbook.getCreationHelper();
  sheet.setDefaultColumnWidth(15);
  
  //创建标题行
  Row titleRow =  sheet.createRow(0);
  Cell titlecell = titleRow.createCell(0);
  CellRangeAddress cellRange = new CellRangeAddress(0,(short)0,0,(short)headCount);
  sheet.addMergedRegion(cellRange);
  titlecell.setCellValue(createHelper.createRichTextString(titleContent));
  titlecell.setCellStyle(title_style);
  //创建列头
  Row header = sheet.createRow(1);
  for(int i=0;i<tableHeadList.size();i++) {
   Cell cell = header.createCell(i);
   cell.setCellValue(createHelper.createRichTextString((String)tableHeadList.get(i)));   
   cell.setCellStyle(head_style); 
  }
 }

public static void createExcel(Workbook workbook,List<String> tableHeadList,Sheet sheet, int startRow)
 {
  CellStyle cellStyle  = workbook.createCellStyle();
  cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
 
  CreationHelper createHelper = workbook.getCreationHelper();
  sheet.setDefaultColumnWidth(15);
  
  Row head = sheet.createRow(startRow);
  for(int i=0;i<tableHeadList.size();i++) {
   Cell cell = head.createCell(i);
   cell.setCellValue(createHelper.createRichTextString((String)tableHeadList.get(i)));
   cell.setCellStyle(cellStyle);
  }
 }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值