|
|
@@ -0,0 +1,196 @@
|
|
|
+package com.goafanti.common.utils;
|
|
|
+
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFFont;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
+
|
|
|
+import com.goafanti.common.model.TOrderBillNew;
|
|
|
+
|
|
|
+public class ExportExcelUtil {
|
|
|
+
|
|
|
+ //设置单元格格式
|
|
|
+ private static HSSFCellStyle setCellStyle1(HSSFWorkbook workbook){
|
|
|
+ // 设置字体
|
|
|
+ HSSFFont font = workbook.createFont();
|
|
|
+ // font.setColor(HSSFFont.COLOR_RED);
|
|
|
+ font.setFontName("微软雅黑");
|
|
|
+ font.setFontHeightInPoints((short) 10);//字体大小
|
|
|
+
|
|
|
+ // 设置样式
|
|
|
+ HSSFCellStyle cellStyle = workbook.createCellStyle();
|
|
|
+ cellStyle.setFont(font); //字体
|
|
|
+
|
|
|
+ //设置自动换行
|
|
|
+ cellStyle.setWrapText(true);
|
|
|
+ return cellStyle;
|
|
|
+ }
|
|
|
+ //设置单元格格式
|
|
|
+ private static HSSFCellStyle setCellStyle2(HSSFWorkbook workbook){
|
|
|
+ // 设置字体
|
|
|
+ HSSFFont font = workbook.createFont();
|
|
|
+ font.setFontName("微软雅黑");
|
|
|
+ font.setFontHeightInPoints((short) 14);//字体大小
|
|
|
+ font.setFontHeight((short) 600);
|
|
|
+ // 设置样式
|
|
|
+ HSSFCellStyle cellStyle = workbook.createCellStyle();
|
|
|
+ cellStyle.setFont(font); //字体
|
|
|
+ return cellStyle;
|
|
|
+ }
|
|
|
+
|
|
|
+ //下载课表
|
|
|
+ public static void download(String path, HttpServletResponse response,String filename) {
|
|
|
+ try {
|
|
|
+ // path是指欲下载的文件的路径。
|
|
|
+ File file = new File(path);
|
|
|
+ // 取得文件名。
|
|
|
+ // 以流的形式下载文件。
|
|
|
+ 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(),"iso-8859-1")); //防止中文乱码或者中文忽略的情况
|
|
|
+ response.addHeader("Content-Length", "" + file.length());
|
|
|
+ OutputStream toClient = new BufferedOutputStream(
|
|
|
+ response.getOutputStream());
|
|
|
+ response.setContentType("application/octet-stream;charset=utf-8");
|
|
|
+ toClient.write(buffer);
|
|
|
+ toClient.flush();
|
|
|
+ toClient.close();
|
|
|
+
|
|
|
+
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ public 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(),"iso-8859-1"));
|
|
|
+ response.addHeader("Content-Length", "" + file.length());
|
|
|
+ OutputStream toClient = new BufferedOutputStream(
|
|
|
+ response.getOutputStream());
|
|
|
+ response.setContentType("application/octet-stream;charset=utf-8");
|
|
|
+ toClient.write(buffer);
|
|
|
+ toClient.flush();
|
|
|
+ toClient.close();
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //=========================================================此处分割线===========================================================================//
|
|
|
+ /**
|
|
|
+ * 导出部门流水数据
|
|
|
+ * @param map 数据
|
|
|
+ * @param deptnames 部门名称数组
|
|
|
+ * @param comment 表头(列名)
|
|
|
+ * @param filePath 生成文件路径
|
|
|
+ */
|
|
|
+ public static void exportDeptBillInfo(Map<String,List<TOrderBillNew>> map,String[] deptnames,String[] comment,String filePath){
|
|
|
+ //map 数据 ,deptnames 部门名称,comment表头 (列名称)
|
|
|
+ //1,创建一个workbook,对应的是一个excel文件
|
|
|
+ HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
+ int count = 0;
|
|
|
+ //遍历map
|
|
|
+ for (Map.Entry<String,List<TOrderBillNew>> entry : map.entrySet()) {
|
|
|
+ //2.创建一个sheet
|
|
|
+ HSSFSheet sheet = wb.createSheet(entry.getKey());
|
|
|
+ //3。创建一个row
|
|
|
+ HSSFRow row = sheet.createRow(0);
|
|
|
+ //4.创建一个cell
|
|
|
+ HSSFCell cell = row.createCell(0);
|
|
|
+ cell.setCellValue(deptnames[count]+" 流水记录");
|
|
|
+ count ++;
|
|
|
+ HSSFCellStyle style1 = setCellStyle1(wb);
|
|
|
+ cell.setCellStyle(style1);
|
|
|
+ //合并列
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, comment.length-1));//合并单元格
|
|
|
+ //创建表头
|
|
|
+ HSSFRow row2 = sheet.createRow(1);
|
|
|
+ for(int i = 0; i < comment.length; i++){
|
|
|
+ sheet.setColumnWidth(i, 3000);//设置列宽
|
|
|
+ HSSFCell c = row2.createCell(i);
|
|
|
+ c.setCellValue(comment[i]);
|
|
|
+ c.setCellStyle(style1);
|
|
|
+ }
|
|
|
+ //填充表格数据
|
|
|
+ //int rowmnum = sheet.getLastRowNum();//获得当前行数据
|
|
|
+ List<TOrderBillNew> list = entry.getValue();
|
|
|
+ if(list == null){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (TOrderBillNew ob : list) {
|
|
|
+ //新增一行
|
|
|
+ HSSFRow r = sheet.createRow(sheet.getLastRowNum()+1);
|
|
|
+ for (int i = 0; i < comment.length; i++) {
|
|
|
+ //当前列的只
|
|
|
+ String o ;
|
|
|
+ switch (comment[i]) {
|
|
|
+ case "平台流水编号": o = ob.getBillNo(); break;
|
|
|
+ case "平台流水时间": o = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ob.getCreateTime()); break;
|
|
|
+ case "交易金额": o = ob.getTransactionAmount().toString(); break;
|
|
|
+ case "付款方": o = ob.getPayerName(); break;
|
|
|
+ case "收款方": o = ob.getPayeeName(); break;
|
|
|
+ case "流水科目": o = ob.getTransactionSubject().toString(); break;
|
|
|
+ case "交易渠道": o = ob.getTransactionChannel().toString(); break;
|
|
|
+ case "交易时间": o = ob.getFinancialPayTimes(); break;
|
|
|
+ case "财务流水号": o = ob.getFinancialPayNo();break;
|
|
|
+ case "订单编号": o = ob.getOrderNo(); break;
|
|
|
+ case "订单负责人": o = ob.getSaleName(); break;
|
|
|
+ default: o = ""; break;
|
|
|
+ }
|
|
|
+ HSSFCell c = r.createCell(i);
|
|
|
+ c.setCellValue(o);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //将表格数据放入文件中
|
|
|
+ try {
|
|
|
+ FileOutputStream fos = new FileOutputStream(filePath);
|
|
|
+ wb.write(fos);
|
|
|
+ fos.close();
|
|
|
+ System.out.println("已经创建文件");
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("发生了异常");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|