java中导出excel

  

依赖:
<!--  excel -->
<dependency>
<groupId>com.innoventsolutions.birt.runtime</groupId>
<artifactId>org.apache.poi_3.9.0.v201405241750</artifactId>
<version>4.8.0</version>
</dependency>

/**
* 创建新excel.
*
* @param fileDir excel的路径
* @param sheetName 要创建的表格索引
*/
public static void createExcel(String fileDir, String sheetName) throws Exception {

HSSFWorkbook workbook = new HSSFWorkbook();
//创建workbook
workbook = new HSSFWorkbook();
//添加Worksheet(不添加sheet时生成的xls文件打开时会报错)
HSSFSheet sheet1 = workbook.createSheet(sheetName);

//新建文件
FileOutputStream out = null;
try {




// sheet1.setColumnWidth(9,5 * 256); 设置第九列的长度为....举个小栗子,如有需要这么设置即可


//以下为excel中样式的设置 但是毕设发现只在具体的单元格中设置才起作用 具体的在行里这么设置不起效果 详见下文
HSSFCellStyle style = workbook.createCellStyle();
style.setWrapText(true);// 设置自动换行
style.setAlignment((short) 2); // 左右居中2 居右3 默认居左
// style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中1

// HSSFCellStyle style = workbook.createCellStyle();
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// HSSFFont font = workbook.createFont();
// style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// font.setFontName("宋体");
// font.setFontHeightInPoints((short) 14);// 设置字体大小


//模板创建
HSSFRow row0 = workbook.getSheet(sheetName).createRow(0);
row0.setHeight((short) (2*255));
System.out.println("第一行行高:"+row0.getHeight());
row0.createCell(15).setCellValue("人");
row0.createCell(16).setCellValue("行");
row0.createCell(17).setCellValue("道");
row0.createCell(18).setCellValue("部");
row0.createCell(19).setCellValue("分");




//样式在这cell里面设置 在row2中设置不起作用
HSSFRow row2 = workbook.getSheet(sheetName).createRow(2);
row2.setHeight((short) (8*255));
HSSFCell cell24 = row2.createCell(4);
cell24.setCellStyle(style);
cell24.setCellValue("啦啦啦德玛西亚");
HSSFCell cell25 = row2.createCell(5);
cell25.setCellStyle(style);
cell25.setCellValue("");
HSSFCell cell26 = row2.createCell(6);
cell26.setCellStyle(style);
cell26.setCellValue("啦啦啦德玛西亚");
HSSFCell cell27 = row2.createCell(7);
cell27.setCellStyle(style);
cell27.setCellValue("啦啦啦德玛西亚");
HSSFCell cell28 = row2.createCell(8);
cell28.setCellStyle(style);
cell28.setCellValue("啦啦啦德玛西亚");
HSSFCell cell29 = row2.createCell(9);
cell29.setCellStyle(style);
cell29.setCellValue("啦啦啦德玛西亚");
HSSFCell cell210 = row2.createCell(10);
cell210.setCellStyle(style);
cell210.setCellValue("啦啦啦德玛西亚");


}





out = new FileOutputStream(fileDir);
workbook.write(out);
} catch (Exception e) {
throw e;
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


应用:
createExcel("D:/template/template","报表明细",maintenanceViewBeans);


栗子:
笔者想实现的功能是前台页面有个表格,然后点击导出按钮 能都导出一个excel 但是想能够在浏览器中弹出一个类似那种下载框一样的展示 可以修改文件名字和导出路径地址,但是一顿搜索发现并没有(大哭)
没办法,只能借用下载的功能框 所以最后实现是这么个流程 当点击导出按钮的时候调用后台接口 先在后台某一个位置写入一个Excel表格 然后调用下载框 在前台选择下载的文件名和路径 实际上是基于
刚刚导出的Excel进行下载一遍
具体代码如下:


/**
* 文件导出
* @param response
*/
@RequestMapping("/exportFile")
public void exportFile(FileExportVM fileExportVM, HttpServletResponse response) throws Exception {


List<MaintenanceViewBean> maintenanceViewBeans = new ArrayList<MaintenanceViewBean>();


ExcelWriteByBridge.createExcel("D:/template/template","报表明细");

String fileName = "Audit_details";
File file = new File("D:/template/template.xlsx");
// 设置强制下载不打开
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream outputStream = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
outputStream.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
//实在不知道怎么能展示出一个弹出框  只能这么实现(笔者前台不太熟,刚刚入职新公司开始使用,如有更好的办法请各位评论给出好建议,小弟不胜感激哈)
posted @ 2019-09-27 15:38  那一年,我二十二  阅读(682)  评论(0)    收藏  举报