html页面表格导出为excel表格实现 excel POI

/**
 * 下载文件,通过html页面直接将表导出
 * 
 * @param response
 * @param request
 * @param fileName
 * @param html
 * @throws Exception
 */
@RequestMapping(params = "downloadFile")
public void downloadFile(HttpServletResponse response, HttpServletRequest request,String fileName,String html,String moneyCol,int headEndRow)
        throws Exception {

    final String userAgent = request.getHeader("USER-AGENT");
    if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器
        fileName = URLEncoder.encode(fileName,"UTF-8");
    }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
        fileName = new String(fileName.getBytes(), "ISO8859-1");
    }else{
        fileName = URLEncoder.encode(fileName,"UTF-8");//其他浏览器
    }
    response.setContentType("application/octet-stream");
    response.addHeader("Content-Disposition", "attachment;" + "filename=\"" + fileName + "\"");
    ServletOutputStream outputStream = response.getOutputStream();
    String cnt =  "\n";
    List<Integer> c =new ArrayList<>();
    if(StringUtils.isNotBlank(moneyCol)){
        String[] split = moneyCol.split(",");
        for (String s : split) {
            c.add(Integer.parseInt(s));
        }
    }
    html = removeBlank(html);
    html = html.replaceAll("&nbsp;", " ");
    html = html.replaceAll("<[\\s]*?br[^>]*?>|<[\\s]*?\\/[\\s]*?br[\\s]*?>", cnt);
    HSSFWorkbook hssfWorkbook = ConvertHtml2Excel.table2Excel(html,c,headEndRow);
    hssfWorkbook.write(outputStream);

}

工具类代码如下:

package cn.piesat.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFCellUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


/****
 * html转excel
 * @author user
 *
 */
public class ConvertHtml2Excel {
    /**
     * html表格转excel
     *
     * @param tableHtml 如
     *                  <table>
     *                  ..
     *                  </table>
     * @return
     */
    public static HSSFWorkbook table2Excel(String tableHtml, List<Integer> moneyCols,int headEndRow) {

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        List<CrossRangeCellMeta> crossRowEleMetaLs = new ArrayList<CrossRangeCellMeta>();
        int rowIndex = 0;
        try {
            Document data = DocumentHelper.parseText(tableHtml);

            HSSFCellStyle contentStyle = getContentStyle(wb);

//            // 生成信息列
//            Element info = data.getRootElement().element("info");
//            if (info != null) {
//                List<Element> trLsb = info.elements("tr");
//                for (Element trEle : trLsb) {
//                    HSSFRow row = sheet.createRow(rowIndex);
//                    List<Element> thLs = trEle.elements("th");
//                    int
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值