java下載自定義的excel文件
public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String fileName= df.format(new Date());
String xlsFile = fileName+".xls";//產生的Excel文件的名稱
List<User> resultList = userDao.QueryUserList();
HSSFWorkbook workbook = new HSSFWorkbook();//產生工作簿對象
//設置表格邊框
HSSFCellStyle style=workbook.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
HSSFSheet sheet = workbook.createSheet("first"); //產生工作表對象,設置第一個工作表的名稱爲firstSheet//爲了工作表能支持中文,設置字符編碼爲UTF_16
workbook.setSheetName(0,"firstSheet");//產生一行
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell = row.createCell(0);
cell.setCellValue("類型1");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("類型2");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("類型3");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("類型4");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("類型5");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("類型6");
cell.setCellStyle(style);
for (int i = 0; i < resultList.size(); i++) {
HSSFRow circle = sheet.createRow(i+1);
cell = circle.createCell(0);
cell.setCellStyle(style);
//控制循環行和列
//判斷產品類型的名稱是否重複,如果重複則名稱爲空
if(resultList.get(i).getName().equals(name)){
cell.setCellValue("");
//System.out.println(i+"空行統計合併");
}else{
name = resultList.get(i).getName();
cell.setCellValue(resultList.get(i).getName());
cell.setCellStyle(style);
//System.out.println(i+"非空行統計合併");
merge = merge + i+",";
}
cell = circle.createCell(1);
cell.setCellStyle(style);
cell.setCellValue(resultList.get(i).getType());
cell = circle.createCell(2);
cell.setCellStyle(style);
cell.setCellValue(weightregion);
cell = circle.createCell(3);
cell.setCellStyle(style);
cell.setCellValue(resultList.get(i)getType1());
cell = circle.createCell(4);
cell.setCellStyle(style);
cell.setCellValue(resultList.get(i).getType2());
cell = circle.createCell(5);
cell.setCellStyle(style);
cell.setCellValue(resultList.get(i).getTypex());
cell = circle.createCell(6);
cell.setCellStyle(style);
}
HSSFRow circle = sheet.createRow(resultList.size()+1);
cell = circle.createCell(0);
cell.setCellValue("彙總1");
cell.setCellStyle(style);
cell = circle.createCell(1);
cell.setCellValue("");
cell.setCellStyle(style);
cell = circle.createCell(2);
cell.setCellValue("--");
cell.setCellStyle(style);
cell = circle.createCell(3);
cell.setCellValue("");
cell.setCellStyle(style);
cell = circle.createCell(4);
cell.setCellValue("");
cell.setCellStyle(style);
cell = circle.createCell(5);
cell.setCellValue("");
cell.setCellStyle(style);
cell = circle.createCell(6);
cell.setCellValue("");
cell.setCellStyle(style);
int mergelength = merge.length();
if(mergelength<=2){
int resultLength = resultList.size()-1;
merge = merge+resultLength+",";
}
merge = merge.substring(2, merge.length()-1);
String[] s = merge.split(",");
int start = 0;
int end = 0;
//產品類型合併單元格
for (int i = 0; i < s.length; i++) {
if(i==0){
start = 1;
}else{
start = Integer.parseInt(s[i-1])+1;
}
if(mergelength<=2){
end = Integer.parseInt(s[i])+1;
}else{
end = Integer.parseInt(s[i]);
}
//System.out.println(start+"--------"+end);
CellRangeAddress cellRangeAddress = new CellRangeAddress(start, end, 0, 0);
sheet.addMergedRegion(cellRangeAddress);
//水平居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//垂直居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
}
// //最後一個類型合併
start = Integer.parseInt(s[s.length-1])+1;
end = resultList.size();
//System.out.println(start+"+++++"+s[s.length-1]+"+++++"+end);
CellRangeAddress cellRangeAddress = new CellRangeAddress(start, end, 0, 0);
//合併單元格
sheet.addMergedRegion(cellRangeAddress);
//合併合計
cellRangeAddress = new CellRangeAddress(resultList.size()+1, resultList.size()+1, 0, 1);
sheet.addMergedRegion(cellRangeAddress);
//水平居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//垂直居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
sheet = workbook.createSheet("two");
workbook.setSheetName(1,"secondSheet");//產生一行
row = sheet.createRow(0);
cell = row.createCell(0);
cell = row.createCell(0);
cell.setCellValue("類型10");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("類型11");
cell.setCellStyle(style);
//以下語句讀取生成的Excel文件內容
xlsFile = resultList.get(0).getCustomername() +" "+ xlsFile;
System.out.println(xlsFile);
//response.setHeader("Content-Disposition","attachment;filename="+xlsFile+"xxxx");//指定下載的文件名
response.setHeader("Content-Disposition", "attachment;filename="+ new String(xlsFile.getBytes(),"iso-8859-1"));
response.setContentType("application/vnd.ms-excel");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
try{
bufferedOutPut.flush();
workbook.write(bufferedOutPut);
bufferedOutPut.close();
}catch(IOException e){
e.printStackTrace();
System.out.println( "Output is closed ");
}
}
org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:"+path);
response.setContentType("application/vnd.ms-excel");
response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.addHeader("charset", "utf-8");
response.addHeader("Pragma", "no-cache");
String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
inputStream = resource.getInputStream();
response.setHeader("Content-Length", String.valueOf(inputStream.available()));//xlsx格式的文件可以下載
servletOutputStream = response.getOutputStream();
IOUtils.copy(inputStream, servletOutputStream);
response.flushBuffer();
以上是xlsx格式的文件下載