java导出Excel并对表头做定制

本文介绍如何使用Java来导出Excel,并详细说明了如何对Excel的表头进行定制,包括设置样式、内容等操作。
摘要由CSDN通过智能技术生成

1、核心代码


package cn.doofen.service.impl;

import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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.ss.util.CellRangeAddress;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cicada.seo.query.impl.QryIntegReportImpl;
import com.doofen.ctrl.rpt.RptConst;
import com.doofen.ctrl.rpt.Impl.RptBase;
import com.zxt.framework.export.RptExcelDomain;

/**
 * 
 * @author Andrew
 *
 * PulishDate: 2015年9月28日
 * Function: 各科平均分比较表
 * ChangeLog:
 */
public class ExportRpt10012Impl extends RptBase{
	
	DecimalFormat df=new DecimalFormat("#.00"); 
	
	


	
	private Object[]  joHeads = null;
	private Object[]  joHeads2 = null;
	
	/**
	 * 构建excel
	 * @param os
	 * @param examId
	 * @throws Exception
	 */
	public void createRptExcel_10012( OutputStream os, Long schId, Long examId , Long clsId, Long gradeId, JSONArray jarr, Long xueke, Long leibie) throws Exception{
		QryIntegReportImpl opr = new QryIntegReportImpl();
		
		String title = null;
		
		if(leibie == 10){
			title = "行政班——"+RptConst.RPT_2000_NAME;
		}else if (leibie == 11){
			title = "选课班——"+RptConst.RPT_2000_NAME;
		}
		
		try{
			JSONArray ja = new JSONArray();
			ja = opr.getExecutiveTable(schId, examId, clsId, gradeId, jarr, xueke, leibie);
			RptExcelDomain rptDo = new RptExcelDom
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java导出Excel二级表头可以使用Apache POI库来实现。以下是一个简单的示例代码: ```java // 创建工作簿对象 Workbook workbook = new XSSFWorkbook(); // 创建工作表对象 Sheet sheet = workbook.createSheet("Sheet1"); // 创建第一行,作为一级表头 Row row1 = sheet.createRow(0); // 创建第一个单元格,作为一级表头第一列 Cell cell11 = row1.createCell(0); cell11.setCellValue("一级表头1"); // 合并一级表头第一列到第三列 CellRangeAddress region1 = new CellRangeAddress(0, 0, 0, 2); sheet.addMergedRegion(region1); // 创建第二个单元格,作为一级表头第四列 Cell cell12 = row1.createCell(3); cell12.setCellValue("一级表头2"); // 合并一级表头第四列到第六列 CellRangeAddress region2 = new CellRangeAddress(0, 0, 3, 5); sheet.addMergedRegion(region2); // 创建第三个单元格,作为一级表头第七列 Cell cell13 = row1.createCell(6); cell13.setCellValue("一级表头3"); // 合并一级表头第七列到第九列 CellRangeAddress region3 = new CellRangeAddress(0, 0, 6, 8); sheet.addMergedRegion(region3); // 创建第二行,作为二级表头 Row row2 = sheet.createRow(1); // 创建二级表头的单元格,根据一级表头的列数来创建 for (int i = 0; i < 9; i++) { Cell cell = row2.createCell(i); cell.setCellValue("二级表头" + (i+1)); } // 创建数据行 for (int i = 2; i < 10; i++) { Row row = sheet.createRow(i); // 创建数据单元格,根据一级表头的列数来创建 for (int j = 0; j < 9; j++) { Cell cell = row.createCell(j); cell.setCellValue("数据" + (i-1) + "-" + (j+1)); } } // 导出Excel文件 try (FileOutputStream outputStream = new FileOutputStream("example.xlsx")) { workbook.write(outputStream); } ``` 在上面的示例代码中,我们首先创建了一个工作簿对象和一个工作表对象。然后,我们创建了第一行作为一级表头,并使用`CellRangeAddress`类来合并单元格。接着,我们创建了第二行作为二级表头,并创建了数据行。最后,我们将工作簿对象导出Excel文件。 请注意,上面的示例代码使用了XSSFWorkbook类来创建工作簿对象,这是POI库中用于创建Excel 2007及以上版本文件的类。如果你要创建Excel 2003版本文件,可以使用HSSFWorkbook类。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值