java根据模板生成Excel文件
来源:互联网 发布:宜兴淘宝照片拍摄 编辑:程序博客网 时间:2024/06/01 20:45
OutputStream outputStream = null;
try {
//模板路径
String templatePath = request.getSession().getServletContext().getRealPath("/")+ "ExcelTemplate\\Excel模板.xls";
//生成的Excel路径
String rootPath = request.getSession().getServletContext().getRealPath("/");
String path = rootPath + "Download";
File filePath =new File(path);
//如果文件夹不存在则创建
if (!filePath .exists() && !filePath .isDirectory())
{
filePath .mkdir();
}
String fileName = filePath + "\\" + (new Date()).getTime() + ".xls";
File file = new File(fileName);
outputStream = new FileOutputStream(file);
templateWrite(outputStream, response, request, lists, templatePath);
//弹出下载框
download(fileName, response);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 根据模板生成Excel
*/
public static void templateWrite(OutputStream outputStream, HttpServletResponse response, HttpServletRequest request, List<Dealer> lists, String templatePath) throws IOException {
//获取所读取excel模板的对象
File fi = new File(templatePath);
if(!fi.exists()){
return;
}
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fi));
// 初始一个workbook
HSSFWorkbook workbook = new HSSFWorkbook(fs);
// 获取sheet
HSSFSheet sheet = workbook.getSheetAt(0);
//获取标题
HSSFRow rowTitle = sheet.getRow(0);
HSSFCellStyle style = workbook.createCellStyle();
rowTitle.setRowStyle(style);
// 创建多行
int rowIndex = 1;
for (Dealer list : lists) {
HSSFRow row = sheet.createRow(rowIndex);
// 创建多列
HSSFCell cell0 = row.createCell(0);
cell0.setCellValue(list.getCode());
HSSFCell cell1 = row.createCell(1);
cell1.setCellValue(list.getName());
HSSFCell cell2 = row.createCell(2);
cell2.setCellValue(list.getShortName());
HSSFCell cell3 = row.createCell(3);
cell3.setCellValue(list.getAddDate().toString());
HSSFCell cell4 = row.createCell(4);
cell4.setCellValue(list.getNote());
rowIndex++;
}
workbook.write(outputStream);
}
/**
* 下载
*/
private static void download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=gb2312");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
- java根据模板生成Excel文件
- Java根据模板生成excel并下载
- 根据模板生成Excel
- 根据Excel模板生成Excel
- NPOI根据Excel模板生成原生的Excel文件实例
- Java根据模板创建excel文件
- Java根据模板创建excel文件
- java根据模板生成word文件
- 根据模板生成word文件
- java根据模板生成pdf文件并导出
- java根据模板生成pdf文件并导出(转)
- java 生成excel根据jxl
- 根据excel或者doc模板生成excel和doc文档
- poi实现根据excel模板,生成excel并导入数据
- JXLS 利用excel模板生成excel文件
- java web根据excel word模板生成前台页面,自动求和
- java poi 生成excel模板
- 根据excel文件生成表的索引
- C语言运算符优先级和口诀
- java随机数
- 深度学习(二)sparse autoencoder的Python实现
- java synchronized关键字
- hibernate注解开发,注解详解
- java根据模板生成Excel文件
- 用一张图解释RxJava中的线程控制
- elipse new class add comments
- 详解Linux下安装配置Nginx
- Python爬虫和情感分析详解
- 在android listview中使用editText无法获取焦点和输入窗
- JAVA基础之HashMap源码(JDK 1.8)
- Hibernate课程下
- HttpClient发送文件与报文