发布网友 发布时间:2022-04-23 10:07
共3个回答
热心网友 时间:2023-10-10 21:03
public FileOutputStream queryImport(String fileName,String excname,HttpServletRequest request,String startime, String endtime,int year) {
FileOutputStream out = null;
// 创建一个新的Excel
HSSFWorkbook workBook = new HSSFWorkbook();
// 创建sheet页
HSSFSheet sheet = workBook.createSheet();
// sheet页名称
workBook.setSheetName(0, excname);
// 创建header页
HSSFHeader header = sheet.getHeader();
// 设置标题居中
header.setCenter("采油一厂数据质量评估报告");
// 设置第一行为Header
// 设置表头样式
HSSFCellStyle headStyle = workBook.createCellStyle();
HSSFFont headFont = workBook.createFont();
headFont.setFontHeightInPoints((short) 12);// 字号
headFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// 加粗
headStyle.setFont(headFont);
headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
headStyle.setTopBorderColor(HSSFColor.BLACK.index);
headStyle.setLeftBorderColor(HSSFColor.BLACK.index);
headStyle.setBottomBorderColor(HSSFColor.BLACK.index);
headStyle.setRightBorderColor(HSSFColor.BLACK.index);
HSSFCellStyle conStyle = workBook.createCellStyle();
HSSFFont headFont1 = workBook.createFont();
headFont1.setFontHeightInPoints((short) 10);// 字号
conStyle.setFont(headFont);
conStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
conStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
conStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
conStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
conStyle.setTopBorderColor(HSSFColor.BLACK.index);
conStyle.setLeftBorderColor(HSSFColor.BLACK.index);
conStyle.setBottomBorderColor(HSSFColor.BLACK.index);
conStyle.setRightBorderColor(HSSFColor.BLACK.index);
Map<String, List<String>> map = tableHeadInfo(year);
// 创建第一行,显示合并表头
HSSFRow fristrow = sheet.createRow(0);
// 第二行,显示列
HSSFRow secrow = sheet.createRow(1);
int startnum = 0;
HSSFCell cellT = null;
List<String> collist = new ArrayList<String>();
if (map != null && map.size() > 0) {
int count = 0;
for (String key : map.keySet()) {
List valueList = map.get(key);
startnum = startnum + valueList.size();
// 获取所有的数据列名称
for (int k = 0; k < valueList.size(); k++) {
collist.add(valueList.get(k).toString());
}
// 第一行,单元格合并显示
if(key.contains("oth")){//列不是合并列
//合并行
Region region1 = new Region(0, (short)((startnum - valueList.size())), 1,(short)(startnum-1));
sheet.addMergedRegion(region1);
cellT = fristrow.createCell((short) ((startnum - valueList.size())));
cellT.setCellValue(key.substring(0, key.length()-3));
cellT.setCellStyle(headStyle);
}else{//列是合并列
Region region1 = new Region(0,(short) ((startnum - valueList.size())), 0,(short) (startnum-1));
sheet.addMergedRegion(region1);
cellT = fristrow.createCell((short) ((startnum - valueList.size())));
cellT.setCellValue(key + "(%)");
cellT.setCellStyle(headStyle);
}
//第二行,表头展现
if (count == (map.size() - 1)) {
for (int j = 0; j < collist.size(); j++) {
cellT = secrow.createCell((short)(j));
cellT.setCellValue(collist.get(j));
cellT.setCellStyle(headStyle);
sheet.setColumnWidth((short)(j), (short) (3000));
}
// 第三行,给单元格填充数据
// 获取结果集
JSONObject json=dataQualityAssessReportDao.getDataQualityAssessReport(startime, endtime);
List<String> listdataList =(List<String>)json.get("strList");
for (int i = 0; i < listdataList.size(); i++) {
//填充数据从第三行开始,所以i要加2
secrow = sheet.createRow((int) i+2);
String strvalue = listdataList.get(i);
String[] arr = strvalue.split(",");
for (int j = 0; j < arr.length; j++) {
cellT = secrow.createCell((short)(j));
if(arr[j].equals("null")){
cellT.setCellValue("");
}else{
//去掉单元格左上角的绿色小三角符号;
//数字类型的数据以字符串类型赋值给单元格就会出现绿三角
if(isNum(arr[j])==true){
cellT.setCellValue(Double.parseDouble(arr[j]));
}else{
cellT.setCellValue(arr[j]);
}
}
cellT.setCellStyle(conStyle);
}
}
}
count++;
}
}
热心网友 时间:2023-10-10 21:03
代码已经写好,请参阅附件【包含所使用到的所有jar包和源代码】JavaExcelDemo.rar\JavaExcelDemo\src\com\test\ExcelCreateTest.java中的main测试函数进行测试。
代码思路简要说明如下:
(1)读取txt文件中的内容
(2)利用json工具类将文件内容解析为java数组对象
(3)遍历数组对象,利用jxl工具类将内容转换为excel文件进行输出
热心网友 时间:2023-10-10 21:04
这是json吧!追问对对!