java编写excel表格

仅供参考,有看不懂的地方@我
 // 声明一个工作薄
    XSSFWorkbook workbook = new XSSFWorkbook();
    // 生成一个表格
    XSSFSheet sheet = workbook.createSheet("sheet");
    // 设置表格默认列宽度为15个字节
    //sheet.setDefaultColumnWidth((short) 20);
    sheet.setColumnWidth(0, 10000);
    sheet.setColumnWidth(1, 3000);
    sheet.setColumnWidth(2, 5000);
    sheet.setColumnWidth(3, 5000);
    sheet.setColumnWidth(4, 5000);

    // 产生表格标题行
    XSSFRow row = sheet.createRow(0);
    XSSFCell cell = row.createCell(0);
    cell.setCellValue("地域");
    XSSFCell cell1 = row.createCell(1);
    cell1.setCellValue("RSRP");
 //合并表格
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 9));
    XSSFCell cell2 = row.createCell(2);
    cell2.setCellValue("CQI");
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 10, 18));


     row = sheet.createRow(2);
    sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 0));
    for(int i=1;i<19;i++){
        XSSFCell cell9 = row.createCell(i);
        if(i%3==1) {
            cell9.setCellValue("总里程");
        }else if(i%3==2){
            cell9.setCellValue("覆盖里程");
        }else{
            cell9.setCellValue("覆盖率");
        }
    }
    int i = 3;
// 赋值
    for(Map map:list){
        row = sheet.createRow(i);
        row.createCell(0).setCellValue(map.get("NAME").toString());
        row.createCell(1).setCellValue(map.get("ZTOTAL_COUNT").toString());
        row.createCell(2).setCellValue(map.get("ZWEAKCOUNT").toString());
        row.createCell(3).setCellValue(map.get("ZRATIO").toString());
        row.createCell(4).setCellValue(map.get("GTOTAL_COUNT").toString());
        row.createCell(5).setCellValue(map.get("GWEAKCOUNT").toString());
        row.createCell(6).setCellValue(map.get("GRATIO").toString());
        row.createCell(7).setCellValue(map.get("UTOTAL_COUNT").toString());
        row.createCell(8).setCellValue(map.get("UWEAKCOUNT").toString());
        row.createCell(9).setCellValue(map.get("URATIO").toString());
        row.createCell(10).setCellValue(map.get("ZZTOTAL_COUNT").toString());
        row.createCell(11).setCellValue(map.get("ZZWEAKCOUNT").toString());
        row.createCell(12).setCellValue(map.get("ZZRATIO").toString());
        row.createCell(13).setCellValue(map.get("GGTOTAL_COUNT").toString());
        row.createCell(14).setCellValue(map.get("GGWEAKCOUNT").toString());
        row.createCell(15).setCellValue(map.get("GGRATIO").toString());
        row.createCell(16).setCellValue(map.get("UUTOTAL_COUNT").toString());
        row.createCell(17).setCellValue(map.get("UUWEAKCOUNT").toString());
        row.createCell(18).setCellValue(map.get("UURATIO").toString());

        i++;
    }

return workbook;
根据excel模板动态导出数据库数据 package text; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet.ServletContext; import net.sf.jxls.transformer.XLSTransformer; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class TextAction extends ActionSupport { /** */ private static final long serialVersionUID = 1L; private String filename; @SuppressWarnings("rawtypes") public String export() throws Exception { String templateFile = "18.xls"; // String sql = "select * from t_ry order by rybm"; // exportAndDownload(templateFile, DataBase.retrieve(sql)); List datas = new ArrayList(); @SuppressWarnings("unchecked") HashMap map = new HashMap(); map.put("name", "1111"); datas.add(map); exportAndDownload(templateFile, datas); return SUCCESS; } @SuppressWarnings({ "rawtypes", "unchecked" }) public void exportAndDownload(String templateFile, List datas) { try { filename = UUID.randomUUID() + templateFile; // FacesContext context = FacesContext.getCurrentInstance(); // ServletContext servletContext = (ServletContext) // context.getExternalContext().getContext(); ServletContext servletContext = ServletActionContext .getServletContext(); String path = servletContext.getRealPath("\\ExcelFile"); String srcFilePath = path + "\\template\\" + templateFile; String destFilePath = path + "\\download\\" + filename; Map beanParams = new HashMap(); beanParams.put("results", datas); XLSTransformer transfer = new XLSTransformer(); transfer.transformXLS(srcFilePath, beanParams, destFilePath); // Browser.execClientScript("window.location.href='../ExcelFile/downloadfile.jsp?filename=" // + destFile + "';"); } catch (Exception e) { e.printStackTrace(); } } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值