package hitown;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
public class T {
public void export() throws FileNotFoundException, IOException {
// 声明String数组,并初始化元素(表头名称)
//第一行表头字段,合并单元格时字段跨几列就将该字段重复几次
String[] excelHeader0 = { "城市名称", "监测点", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)",
"污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)",
"污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)",
"污染物浓度及空气质量分指数(AQI)", "空气质量指数(AQI)", "首要污染物", "空气质量指数级别", "空气质量指数类别", "空气质量指数类别" };
// “0,2,0,0” ===> “起始行,截止行,起始列,截止列”
String[] headnum0 = { "0,2,0,0", "0,2,1,1", "0,0,2,13", "0,2,14,14", "0,2,15,15", "0,2,16,16", "0,1,17,18" };
//第二行表头字段,其中的空的双引号是为了补全表格边框
String[] excelHeader1 = { "二氧化硫(SO₂)24小时平均", "二氧化硫(SO₂)24小时平均", "二氧化氮(NO₂)24小时平均", "二氧化氮(NO₂)24小时平均",
"颗粒物(粒径小于等于10μm)24小时平均", "颗粒物(粒径小于等于10μm)24小时平均", "一氧化碳(CO)24小时平均", "一氧化碳(CO)24小时平均", "臭氧(O₃)最大8小时平均",
"臭氧(O₃)最大8小时平均", "颗粒物(粒径小于等于2.5μm)24小时平均", "颗粒物(粒径小于等于2.5μm)24小时平均","","","","","" };
// 合并单元格
String[] headnum1 = { "1,1,2,3", "1,1,4,5", "1,1,6,7", "1,1,8,9", "1,1,10,11", "1,1,12,13" };
//第三行表头字段
String[] excelHeader2 = { "", "", "浓度/(μg/m3)", "分指数", "浓度/(μg/m3)", "分指数", "浓度/(μg/m3)", "分指数", "浓度/(μg/m3)", "分指数",
"浓度/(μg/m3)", "分指数", "浓度/(μg/m3)", "分指数","", "类别", "颜色" };
String[] headnum2 = { "2,2,2,2", "2,2,3,3", "2,2,4,4", "2,2,5,5", "2,2,6,6", "2,2,7,7", "2,2,8,8", "2,2,9,9",
"2,2,10,10", "2,2,11,11", "2,2,12,12", "2,2,13,13", "2,2,17,17", "2,2,18,18" };
// 声明一个工作簿
HSSFWorkbook wb = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = wb.createSheet("TAQIDataReport");
// 生成一种样式
HSSFCellStyle style = wb.createCellStyle();
// 设置样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成一种字体
HSSFFont font = wb.createFont();
// 设置字体
font.setFontName("微软雅黑");
// 设置字体大小
font.setFontHeightInPoints((short) 12);
// 字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 在样式中引用这种字体
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = wb.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一种字体2
HSSFFont font2 = wb.createFont();
// 设置字体
font2.setFontName("微软雅黑");
// 设置字体大小
font2.setFontHeightInPoints((short) 12);
// 字体加粗
// font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 在样式2中引用这种字体
style2.setFont(font2);
// 生成表格的第一行
// 第一行表头
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < excelHeader0.length; i++) {
sheet.autoSizeColumn(i, true);// 根据字段长度自动调整列的宽度
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelHeader0[i]);
cell.setCellStyle(style);
// System.out.println(excelHeader0[i]);
if (i >= 0 && i <= 18) {
for (int j = 0; j < excelHeader0.length; j++) {
// 从第j列开始填充
cell = row.createCell(j);
// 填充excelHeader1[j]第j个元素
cell.setCellValue(excelHeader0[j]);
cell.setCellStyle(style);
}
}
// 设置列宽
// sheet.setColumnWidth(0, 5500);
// sheet.setColumnWidth(1, 6500);
// sheet.setColumnWidth(2, 6500);
// sheet.setColumnWidth(3, 6000);
// sheet.setColumnWidth(4, 6500);
// sheet.setColumnWidth(5, 6500);
// sheet.setColumnWidth(6, 6500);
// sheet.setColumnWidth(7, 6500);
// sheet.setColumnWidth(8, 6500);
// sheet.setColumnWidth(9, 6500);
// sheet.setColumnWidth(10, 6500);
// sheet.setColumnWidth(11, 6500);
// sheet.setColumnWidth(12, 6500);
// sheet.setColumnWidth(13, 6500);
// sheet.setColumnWidth(14, 6500);
// sheet.setColumnWidth(15, 6500);
// sheet.setColumnWidth(16, 6500);
// sheet.setColumnWidth(17, 6500);
// sheet.setColumnWidth(18, 6500);
// sheet.setDefaultRowHeight((short) 360);// 设置行高
}
// 动态合并单元格
for (int i = 0; i < headnum0.length; i++) {
sheet.autoSizeColumn(i, true);
String[] temp = headnum0[i].split(",");
Integer startrow = Integer.parseInt(temp[0]);
Integer overrow = Integer.parseInt(temp[1]);
Integer startcol = Integer.parseInt(temp[2]);
Integer overcol = Integer.parseInt(temp[3]);
sheet.addMergedRegion(new CellRangeAddress(startrow, overrow, startcol, overcol));
}
// 第二行表头
row = sheet.createRow(1);
for (int i = 0; i < excelHeader1.length; i++) {
sheet.autoSizeColumn(i, true);// 自动调整宽度
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
TestExcel.rar (21个子文件)
TestExcel
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 2KB
lib
poi-examples-3.9-20121203.jar 302KB
poi-ooxml-schemas-3.9-20121203.jar 4.58MB
poi-ooxml-3.9-20121203.jar 915KB
poi-excelant-3.9-20121203.jar 30KB
poi-3.9-20121203.jar 1.78MB
poi-scratchpad-3.9-20121203.jar 1.17MB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 305B
org.eclipse.core.resources.prefs 97B
org.eclipse.wst.common.component 464B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 521B
org.eclipse.core.runtime.prefs 52B
.jsdtscope 567B
org.eclipse.jdt.core.prefs 364B
src
hitown
T.java 10KB
.project 1KB
.classpath 1KB
build
classes
hitown
T.class 6KB
共 21 条
- 1
资源评论
珞珈王
- 粉丝: 19
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功