所需jar包 https://download.****.net/my
@Override
public InputStream getExcelUserId(int id,User user) {
// TODO Auto-generated method stub
HSSFWorkbook book = new HSSFWorkbook();
//获取审核人数
String useName=user.getUsername();//获取审核人员名称
// 创建一个sheet,并命名
HSSFSheet sheet = book.createSheet(useName);
// 1.生成字体对象
HSSFFont font = book.createFont();
font.setFontHeightInPoints((short) 10);
font.setFontName("新宋体");
font.setColor(HSSFColor.BLUE.index);
font.setBoldweight((short) 0.8);
// 2.生成样式对象
HSSFCellStyle style = book.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setFont(font); // 调用字体样式对象
style.setWrapText(true);
String [] headers={"一级标题","权重","二级标题","权重","三级标题","权重","评价要点","计分方法","数据来源","打分"};
//生成标题,第一行标题默认下标为:0,这里 sheet.createRow(0);创建第一行
HSSFRow row = sheet.createRow(0); // 产生表格标题行
for (int j = 0; j < headers.length; j++) {
HSSFCell cell = row.createCell(j);
HSSFRichTextString text = new HSSFRichTextString(headers[j]);
cell.setCellValue(text);
}
//循环赋值,先创建所有单元格,要合并的单元个再创建完成后再获取单元格赋值并合并
List<WebThreeIndex> listThree=webDao.getWebThreeIndexImage(id,user.getId());
if(listThree.size()>0&&listThree!=null){
for(int t=0;t<listThree.size();t++){
WebThreeIndex webThreeIndex=listThree.get(t);
HSSFRow row1 = sheet.createRow(t+1);//三级标题名称数据
HSSFCell hc0 = row1.createCell(0);
//这里是没有写入数据的单元个,如果后面想要获取此处单元格并赋值就必须给一个值
hc0.setCellValue(new HSSFRichTextString(" "));
HSSFCell hc1 = row1.createCell(1);
hc1.setCellValue(new HSSFRichTextString(" "));
HSSFCell hc2 = row1.createCell(2);
hc2.setCellValue(new HSSFRichTextString(" "));
HSSFCell hc3 = row1.createCell(3);
hc3.setCellValue(new HSSFRichTextString(" "));
HSSFCell hc4 = row1.createCell(4);
hc4.setCellValue(new HSSFRichTextString(webThreeIndex.getThree_index()));
HSSFCell hc5 = row1.createCell(5);
hc5.setCellValue(new HSSFRichTextString(webThreeIndex.getWeight()));
HSSFCell hc6 = row1.createCell(6);
hc6.setCellValue(new HSSFRichTextString(webThreeIndex.getEvaluate()));
HSSFCell hc7 = row1.createCell(7);
hc7.setCellValue(new HSSFRichTextString(webThreeIndex.getScoring_method()));
HSSFCell hc8 = row1.createCell(8);
hc8.setCellValue(new HSSFRichTextString(webThreeIndex.getData_sources()));
HSSFCell hc9 = row1.createCell(9);
hc9.setCellStyle(style);
hc9.setCellValue(new HSSFRichTextString(webThreeIndex.getSjscore()));
}
}
//创建单元个结束
//给一级标题和权重赋值并合并
List<WebOneIndex> listOne=webDao.getWebOneIndex();//获取一级标题数据
if(listOne.size()>0&&listOne!=null){
int n=1;
for(int j=0;j<listOne.size();j++){
WebOneIndex webOneIndex=listOne.get(j);
List<WebThreeIndex> threeList=webDao.getWebThreeIndex(webOneIndex.getId());//获取三级级指标数据
int lie=threeList.size();//获取一级指标所跨行数
HSSFRow row5 = sheet.getRow(n);
if(j==0){//第一次是从下标为1开始,0是标题
//CellRangeAddress(1, lie, 0, 0)) 开始行,结束行,开始列,结束列
sheet.addMergedRegion(new CellRangeAddress(1, lie, 0, 0));
HSSFCell cell0 = row5.getCell(0);// 获得第五行的第四个单元格
cell0.setCellStyle(style);
cell0.setCellValue(webOneIndex.getOne_index());// 给单元格赋值
sheet.addMergedRegion(new CellRangeAddress(1, lie, 1, 1));
HSSFCell cell1 = row5.getCell(1);
cell1.setCellStyle(style);
cell1.setCellValue(webOneIndex.getWeight());
n+=lie;
}else{
sheet.addMergedRegion(new CellRangeAddress(n, n+lie-1, 0, 0));
HSSFCell cell0 = row5.getCell(0);// 获得第五行的第四个单元格
cell0.setCellStyle(style);
cell0.setCellValue(webOneIndex.getOne_index());// 给单元格赋值
sheet.addMergedRegion(new CellRangeAddress(n, n+lie-1, 1, 1));
HSSFCell cell1 = row5.getCell(1);
cell1.setCellStyle(style);
cell1.setCellValue(webOneIndex.getWeight());
n+=lie;
}
}
}
List<WebTwoIndex> listTwo=webDao.getWebTwoIndex();
if(listTwo.size()>0&&listTwo!=null){
int n1=1;
for(int j=0;j<listTwo.size();j++){
WebTwoIndex WebTwoIndex=listTwo.get(j);
List<WebThreeIndex> threeList=webDao.getWebThreeIndexTwoId(WebTwoIndex.getId());
int lie=threeList.size();//获取一级指标所跨行数
HSSFRow row5 = sheet.getRow(n1); // 获得工作薄的第五行
if(j==0){
sheet.addMergedRegion(new CellRangeAddress(1, n1+lie-1, 2, 2));
HSSFCell cell0 = row5.getCell(2);// 获得第五行的第四个单元格
cell0.setCellStyle(style);
cell0.setCellValue(WebTwoIndex.getTwo_index());// 给单元格赋值
sheet.addMergedRegion(new CellRangeAddress(1, n1+lie-1, 3, 3));
HSSFCell cell1 = row5.getCell(3);
cell1.setCellStyle(style);
cell1.setCellValue(WebTwoIndex.getWeight());
n1+=lie;
}else{
sheet.addMergedRegion(new CellRangeAddress(n1, n1+lie-1, 2, 2));
HSSFCell cell0 = row5.getCell(2);// 获得第五行的第四个单元格
cell0.setCellStyle(style);
cell0.setCellValue(WebTwoIndex.getTwo_index());// 给单元格赋值
sheet.addMergedRegion(new CellRangeAddress(n1, n1+lie-1, 3, 3));
HSSFCell cell1 = row5.getCell(3);
cell1.setCellStyle(style);
cell1.setCellValue(WebTwoIndex.getWeight());
n1+=lie;
}
}
}
Long time = System.currentTimeMillis();
String newFileName = String.valueOf(time)+".xlsx";//文件重命名,防止重复文件名
Properties prop = new Properties();
InputStream input = null;
String fileP = "";
try {
input = this.getClass().getResourceAsStream("/apiconfig.properties");// new
prop.load(input);
fileP = prop.getProperty("exportExcel") ;
} catch (Exception e) {
fileP = "C:/workspacess/zjts/UploadFile/";
}
File dir=new File(fileP);
//判断文件是否上传,如果上传的话将会创建该目录
if(!dir.exists()){
dir.mkdirs(); //创建该目录
}
File file = new File(fileP + newFileName);
try {
OutputStream output = new FileOutputStream(file);
book.write(output);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
// 返回结果的文件输入文件流,到内存里方便下载
InputStream input1 = null;
try {
input1 = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//删除生成的文件,减少服务器空间
if(file.exists()){
// file.delete();
}
return input1;
}
//上传样式