public FileOutputStream queryImport(String fileName,String excname,HttpServletRequest request,String startime, String endtime,int year) { FileOutputStream out = null; // Create a new Excel HSSFWorkbook workBook = new HSSFWorkbook(); // Create a sheet page HSSFSheet sheet = workBook.createSheet(); // The sheet page name workBook.setSheetName(0, excname); // Create a header page HSSFHeader header = sheet.getHeader(); // Set the title to be centered header.setCenter ("Data Quality Assessment Report of the First Oil Production Plant"); // Set the first row Header // Set the header style HSSFCellStyle headStyle = workBook.createCellStyle(); HSSFFont headFont = workBook.createFont(); headFont.setFontHeightInPoints((short) 12);// Font size headFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);// Bold headStyle.setFont(headFont); headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// Center left and right headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// Center top and bottom headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); headStyle.setTopBorderColor(HSSFColor.BL ACK.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);//Font size conStyle.setFont(headFont); conStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); conStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); conStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); conStyle.set BorderRight (HSSFCellStyle.BORDER_THIN); conStyle.setTopBorderColor(HSSFColor.BLACK.index); conStyle.setLeftBorderColor(HSSFColor.BLACK.index); conStyle.setBottomBorderColor(HSSFColor.BLACK.index); conStyle.setRightBorderColor(HSSFColor.BLACK.index); Map
for (String key : map.keySet()) {
List valueList = map.get(key);
startnum = startnum + valueList.size();
// Get all data column names
for (int k = 0; k < valueList.size(); k++) { collist.add(valueList.get(k).toString());
}
// The first row, the cells are merged and displayed
if( key.contains("oth")){//Column is not a merged column//Merge row 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{//The column is a merged column 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);
}
//Second line, header display
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)); } // The third line, fill the cell with data // Get the result set JSONObject json=dataQualityAssessReportDao. getDataQualityAssessReport(startime, endtime); List
cellT = secrow.createCell((short)(j));
if(arr[j] .equals("null")){
cellT.setCellValue("");
}else{
//Remove the green color in the upper left corner of the cell Small triangle symbol;
//When numeric type data is assigned to a cell in string type, a green triangle will appear
if(isNum(arr[j])==true) { cellT.setCellValue(Double.parseDouble(arr[j]));
}else{ cellT.setCellValue(arr[j]);
}
}
cellT.setCellStyle(conStyle); } }
}
count++;
} }?I will give you this example , which means it is similar to what you requested, you can modify it yourself.