当前位置:   article > 正文

(新手笔记分享)Spring MVC根据查询生成Excel(导出数据+图片)以流的方式返回给前端下载- 摘取片段,仅供参考_springmvc 生成excel写入响应流

springmvc 生成excel写入响应流

查询的效果图

表格数据结构

查看的表格效果图

导出表格的Excel效果图

控制层:

  1. @ResponseBody
  2. @RequestMapping(value = "excleDownload")
  3. public void excleDownload(HttpServletRequest request, HttpServletResponse response,Integer id) throws IOException {
  4. List<ljCheckInstanceM> ljListInfo = dailyInService.queryContentInfo(id);//查询内容 ljCheckInstanceM是实体类
  5. List<ljCheckInstanceM> ljListPhoto = dailyInService.queryPhotoInfo(id);//查询id下对应的图片
  6. Map<String, String> fileQD = ExportExcelUtil.excleDownload(request.getServletContext(), null, ljListInfo, ljListPhoto);//生成Excel文件并返回地址
  7. String excelPathP = fileQD.get("path");//路径
  8. String excelNameP = fileQD.get("name");//名称
  9. //文件流的方式导出Excel
  10. try {
  11. // 输出响应正文的输出流
  12. OutputStream out;
  13. // 读取本地文件的输入流
  14. InputStream in;
  15. // 获得本地输入流
  16. File file = new File("C:\\"+ excelPathP + excelNameP);
  17. in = new FileInputStream(file);
  18. // 设置响应正文的MIME类型
  19. response.setContentType("application/octet-stream;charset=UTF-8");
  20. String fileName = new String(excelNameP.getBytes("gb2312"), "iso8859-1");
  21. response.setHeader("Content-disposition", "attachment;filename=" + fileName);
  22. // 把本地文件发送给客户端
  23. out = response.getOutputStream();
  24. int byteRead = 0;
  25. byte[] buffer = new byte[512];
  26. while ((byteRead = in.read(buffer)) != -1) {
  27. out.write(buffer, 0, byteRead);
  28. }
  29. in.close();
  30. out.flush();
  31. out.close();
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. logger.error("文件下载出现异常", e);
  35. }
  36. }

生成Excel的工具类

  1. package com.cyl.util;
  2. import java.awt.image.BufferedImage;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.UnsupportedEncodingException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import javax.imageio.ImageIO;
  14. import javax.servlet.ServletContext;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.apache.log4j.Logger;
  17. import org.apache.poi.ss.usermodel.Cell;
  18. import org.apache.poi.ss.usermodel.CellStyle;
  19. import org.apache.poi.ss.util.CellRangeAddress;
  20. import org.apache.poi.ss.util.RegionUtil;
  21. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  22. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  23. import org.apache.poi.xssf.usermodel.XSSFDrawing;
  24. import org.apache.poi.xssf.usermodel.XSSFFont;
  25. import org.apache.poi.xssf.usermodel.XSSFRow;
  26. import org.apache.poi.xssf.usermodel.XSSFSheet;
  27. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  28. import com.cyl.model.ljCheckBodyTarget;
  29. import com.cyl.model.ljCheckInstanceM;
  30. public class ExportExcelUtil {
  31. private static Logger log = Logger.getLogger(ExportExcelUtil.class);
  32. /**
  33. * 导出Excel
  34. * @param context 上下文会话对象
  35. * @param dicument 生成文件的目錄文件名,參數為空時默認為excel
  36. * @param ljListInfo 类数据集
  37. * @param ljListPhoto 类数据集-图片地址
  38. * @param sheetName sheet名称
  39. * @param title 标题
  40. * @return
  41. * @throws UnsupportedEncodingException
  42. */
  43. public static Map<String, String> excleDownload(ServletContext context, String dicument,
  44. List<ljCheckInstanceM> ljListInfo, List<ljCheckInstanceM> ljListPhoto) {
  45. Map<String, String> result = new HashMap<String, String>();
  46. //创建工作薄
  47. XSSFWorkbook wb = new XSSFWorkbook();
  48. //创建表格
  49. XSSFSheet sheet = wb.createSheet();
  50. //设置列宽
  51. sheet.setColumnWidth(0,4000);
  52. sheet.setColumnWidth(1,5500);
  53. sheet.setColumnWidth(2,5700);
  54. sheet.setColumnWidth(3,5700);
  55. sheet.setColumnWidth(4,3500);
  56. sheet.setColumnWidth(5,3500);
  57. sheet.setColumnWidth(6,3500);
  58. sheet.setColumnWidth(7,3500);
  59. sheet.setColumnWidth(8,3500);
  60. sheet.setColumnWidth(9,3500);
  61. /*************边框、居中、加粗、仿宋用于 标题样式********************/
  62. //创建样式1
  63. XSSFCellStyle style = wb.createCellStyle();
  64. //设置背景色
  65. style.setFillForegroundColor((short)71);//浅蓝色
  66. style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
  67. //设置边框样式
  68. style.setBorderBottom(CellStyle.BORDER_THIN);
  69. style.setBorderLeft(CellStyle.BORDER_THIN);
  70. style.setBorderRight(CellStyle.BORDER_THIN);
  71. style.setBorderTop(CellStyle.BORDER_THIN);
  72. // 指定单元格居中对齐
  73. style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
  74. // 指定单元格垂直居中对齐
  75. style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
  76. //设置字体
  77. XSSFFont font = wb.createFont();
  78. font.setFontName("仿宋_GB2312");//设置字体样式
  79. font.setColor((short)1);//设置字体颜色
  80. font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);//粗体显示
  81. font.setFontHeightInPoints((short) 12);
  82. style.setFont(font);//选择需要用到的字体格式
  83. /****************结束******************/
  84. /*************边框、居中、加粗、仿宋用于 副标题样式********************/
  85. //创建样式1
  86. XSSFCellStyle style1 = wb.createCellStyle();
  87. //设置样式
  88. style1.setBorderBottom(CellStyle.BORDER_THIN);
  89. style1.setBorderLeft(CellStyle.BORDER_THIN);
  90. style1.setBorderRight(CellStyle.BORDER_THIN);
  91. style1.setBorderTop(CellStyle.BORDER_THIN);
  92. // 指定单元格居中对齐
  93. style1.setAlignment(XSSFCellStyle.ALIGN_CENTER);
  94. // 指定单元格垂直居中对齐
  95. style1.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
  96. //设置字体
  97. XSSFFont font1 = wb.createFont();
  98. font1.setFontName("仿宋_GB2312");
  99. font1.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);//粗体显示
  100. font1.setFontHeightInPoints((short) 12);
  101. style1.setFont(font1);//选择需要用到的字体格式
  102. /****************结束******************/
  103. /****************用于内容设置样式******************/
  104. XSSFCellStyle style2 = wb.createCellStyle();//创建样式2
  105. style2.setBorderBottom(CellStyle.BORDER_THIN);
  106. style2.setBorderLeft(CellStyle.BORDER_THIN);
  107. style2.setBorderRight(CellStyle.BORDER_THIN);
  108. style2.setBorderTop(CellStyle.BORDER_THIN);
  109. style2.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
  110. style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
  111. style2.setWrapText(true);//自动换行
  112. //设置字体
  113. XSSFFont font2 = wb.createFont();
  114. font2.setFontName("仿宋_GB2312");
  115. font2.setFontHeightInPoints((short) 12);
  116. style2.setFont(font2);//选择需要用到的字体格式
  117. /****************结束******************/
  118. /****************用于内容设置样式3******************/
  119. XSSFCellStyle style3 = wb.createCellStyle();//创建样式2
  120. style3.setFillForegroundColor((short)70);//浅灰色 颜色参考地址 https://blog.csdn.net/w779050550/article/details/81094221
  121. style3.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
  122. style3.setBorderBottom(CellStyle.BORDER_THIN);//设置边框
  123. style3.setBorderLeft(CellStyle.BORDER_THIN);
  124. style3.setBorderRight(CellStyle.BORDER_THIN);
  125. style3.setBorderTop(CellStyle.BORDER_THIN);
  126. style3.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐
  127. style3.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐
  128. //设置字体
  129. XSSFFont font3 = wb.createFont();
  130. font3.setFontName("仿宋_GB2312");
  131. font3.setFontHeightInPoints((short) 12);
  132. style3.setFont(font3);//选择需要用到的字体格式
  133. /****************结束******************/
  134. //行
  135. XSSFRow row = null;
  136. FileOutputStream out = null;
  137. String fileName = TUtil.format("yyyy_MM_dd_HHmmssSSSSSS");
  138. // 日期格式转为字符串输出
  139. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  140. String filePath = null;//路径
  141. CellRangeAddress region = null;//合并单元格
  142. Cell cell = null;
  143. BufferedImage bufferImg = null;
  144. int index = 1;//序号计数
  145. try {
  146. row = sheet.createRow(0);//Excel表格第一行
  147. cell = row.createCell(0);
  148. cell.setCellStyle(style);
  149. cell.setCellValue("详细信息");
  150. //合并单元格 first row last row first column last column
  151. region = new CellRangeAddress(0, 0, 0, 8 );
  152. sheet.addMergedRegion(region);
  153. setBorderStyle(XSSFCellStyle.BORDER_THIN, region, sheet, wb); //给合并过的单元格加边框
  154. Map<String, Object> needmegMap = new HashMap<String,Object>();//为了大项合并,name标记大项值,firstRow标记在Excel中的行号
  155. Map<String, Object> needmegMapSub = new HashMap<String,Object>();//为了子项合并,name标记大项值,firstRow标记在Excel中的行号
  156. for (int i = 0; i < ljListInfo.size(); i++) {
  157. ljCheckInstanceM data = ljListInfo.get(i);
  158. if (i == 0) {
  159. //为了合并大项 初始化标记的map
  160. needmegMap.put("firstRow", i + 3);//firstRow标记在Excel中的行号
  161. needmegMap.put("name", "");//name标记大项值
  162. needmegMapSub.put("firstRow", i + 3);//firstRow标记在Excel中的行号
  163. needmegMapSub.put("name", "");//name标记子项值
  164. fileName += "_"+ data.getSignature();//以时间+小区(单位)名为文件名
  165. fileName += ".xlsx";
  166. row = sheet.createRow(1);//Excel表格第二行
  167. cell = row.createCell(0);
  168. cell.setCellStyle(style1);
  169. cell.setCellValue("镇/街道");
  170. cell = row.createCell(1);
  171. cell.setCellStyle(style2);
  172. cell.setCellValue(data.getAttr1());
  173. cell = row.createCell(2);
  174. cell.setCellStyle(style1);
  175. cell.setCellValue("小区(单位)名");
  176. cell = row.createCell(3);
  177. cell.setCellStyle(style2);
  178. cell.setCellValue(data.getSignature());
  179. cell = row.createCell(4);
  180. cell.setCellStyle(style1);
  181. cell.setCellValue("总得分");
  182. region = new CellRangeAddress(1, 1, 4, 5 );
  183. sheet.addMergedRegion(region);
  184. setBorderStyle(XSSFCellStyle.BORDER_THIN, region, sheet, wb); //给合并过的单元格加边框
  185. cell = row.createCell(6);
  186. cell.setCellStyle(style2);
  187. cell.setCellValue(data.getTotalScore());
  188. cell = row.createCell(7);
  189. cell.setCellStyle(style1);
  190. cell.setCellValue("检查日期");
  191. String endTIme = sdf.format(data.getCheckDate());
  192. cell = row.createCell(8);
  193. cell.setCellStyle(style2);
  194. cell.setCellValue(endTIme);
  195. row = sheet.createRow(2);//Excel表格第三行
  196. cell = row.createCell(0);
  197. cell.setCellStyle(style1);
  198. cell.setCellValue("序号");
  199. cell = row.createCell(1);
  200. cell.setCellStyle(style1);
  201. cell.setCellValue("检查内容");
  202. cell = row.createCell(2);
  203. cell.setCellStyle(style1);
  204. cell.setCellValue("检查子项");
  205. cell = row.createCell(3);
  206. cell.setCellStyle(style1);
  207. cell.setCellValue("指标");
  208. cell = row.createCell(4);
  209. cell.setCellStyle(style1);
  210. cell.setCellValue("评分");
  211. cell = row.createCell(5);
  212. cell.setCellStyle(style1);
  213. cell.setCellValue("说明");
  214. cell = row.createCell(6);
  215. cell.setCellStyle(style1);
  216. cell.setCellValue("图片");
  217. region = new CellRangeAddress(2, 2, 6, 8);
  218. sheet.addMergedRegion(region);
  219. setBorderStyle(XSSFCellStyle.BORDER_THIN, region, sheet, wb); //给合并过的单元格加边框
  220. }
  221. if(i == 0 || i > 0){
  222. row = sheet.createRow(i + 3);//动态创建第四行...N行
  223. //检查内容 大项合并行
  224. cell = row.createCell(1);
  225. cell.setCellStyle(style2);
  226. cell.setCellValue(data.getAttr2());
  227. if (!needmegMap.get("name").equals(data.getAttr2())) {
  228. cell = row.createCell(0);
  229. cell.setCellStyle(style2);
  230. cell.setCellValue(index++);
  231. if (!needmegMap.get("name").equals("")) {
  232. region = new CellRangeAddress((int) needmegMap.get("firstRow"), i + 3 - 1, 0, 0);//合并单元格(开始行,结束行,开始列,结束列)
  233. sheet.addMergedRegion(region);
  234. region = new CellRangeAddress((int) needmegMap.get("firstRow"), i + 3 - 1, 1, 1);
  235. sheet.addMergedRegion(region);
  236. setBorderStyle(XSSFCellStyle.BORDER_THIN, region, sheet, wb); //给合并过的单元格加边框
  237. }
  238. needmegMap.put("firstRow", i + 3);
  239. needmegMap.put("name", data.getAttr2());
  240. }
  241. if (i == ljListInfo.size() - 1) {//最后一个合并
  242. if (!needmegMap.get("name").equals("")) {
  243. region = new CellRangeAddress((int) needmegMap.get("firstRow"), ljListInfo.size() + 2, 0, 0);
  244. sheet.addMergedRegion(region);
  245. setBorderStyle(XSSFCellStyle.BORDER_THIN, region, sheet, wb); //给合并过的单元格加边框
  246. region = new CellRangeAddress((int) needmegMap.get("firstRow"), ljListInfo.size() + 2, 1, 1);
  247. sheet.addMergedRegion(region);
  248. setBorderStyle(XSSFCellStyle.BORDER_THIN, region, sheet, wb); //给合并过的单元格加边框
  249. }
  250. }
  251. cell = row.createCell(2);
  252. cell.setCellStyle(style2);
  253. cell.setCellValue(data.getAttr3());
  254. if (!needmegMapSub.get("name").equals(data.getAttr3())) {
  255. if (!needmegMapSub.get("name").equals("")) {
  256. region = new CellRangeAddress((int) needmegMapSub.get("firstRow"), i + 3 - 1, 2, 2);
  257. sheet.addMergedRegion(region);
  258. setBorderStyle(XSSFCellStyle.BORDER_THIN, region, sheet, wb); //给合并过的单元格加边框
  259. }
  260. needmegMapSub.put("firstRow", i + 3);
  261. needmegMapSub.put("name", data.getAttr3());
  262. }
  263. if (i == ljListInfo.size() - 1) {//最后一个合并
  264. if (!needmegMapSub.get("name").equals("")) {
  265. region = new CellRangeAddress((int) needmegMapSub.get("firstRow"), ljListInfo.size() + 2, 2, 2);
  266. sheet.addMergedRegion(region);
  267. setBorderStyle(XSSFCellStyle.BORDER_THIN, region, sheet, wb); //给合并过的单元格加边框
  268. }
  269. }
  270. cell = row.createCell(3);
  271. cell.setCellStyle(style2);
  272. cell.setCellValue(data.getAttr4());
  273. cell = row.createCell(4);
  274. cell.setCellValue(data.getsScore());
  275. double sScore = data.getStandardScore();
  276. double Score = data.getsScore();
  277. if (sScore > Score) {
  278. cell.setCellStyle(style3);
  279. }else {
  280. cell.setCellStyle(style2);
  281. }
  282. cell = row.createCell(5);
  283. cell.setCellStyle(style2);
  284. cell.setCellValue(data.getSignature2());
  285. cell = row.createCell(6);
  286. cell.setCellStyle(style2);
  287. cell = row.createCell(7);
  288. cell.setCellStyle(style2);
  289. cell = row.createCell(8);
  290. cell.setCellStyle(style2);
  291. }
  292. if (data.getAttr6() != null) {
  293. int col = 6;//记录当前开始列
  294. for (int j = 0; j < ljListPhoto.size(); j++) {
  295. ljCheckInstanceM dataPh = ljListPhoto.get(j);
  296. if (data.getAttr6().equals(dataPh.getAttr2())) {
  297. row.setHeight((short)1500);//设置行高度
  298. // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
  299. ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
  300. try {
  301. String file = "C:\\" + dataPh.getAttr4();//获取地址
  302. bufferImg = ImageIO.read(new File(file));
  303. ImageIO.write(bufferImg, "png", byteArrayOut);
  304. } catch (IOException e) {
  305. //e.printStackTrace();
  306. continue;
  307. }
  308. XSSFDrawing patriarch = sheet.createDrawingPatriarch();
  309. XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, col, i + 3, col + 1, i + 4);
  310. // 插入图片
  311. patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_JPEG));
  312. sheet.setColumnWidth(col, 256 * 20);//列宽
  313. col++;
  314. }
  315. }
  316. }
  317. }
  318. String relpath = (StringUtils.isBlank(dicument) ? "excel" : dicument) + "/";
  319. filePath = "C:\\" + relpath;// 文件存放路径
  320. File fileDir = new File(filePath);
  321. if (!(fileDir.exists() && fileDir.isDirectory())) {
  322. new File(filePath).mkdirs();
  323. }
  324. result.put("path", relpath);
  325. result.put("name", fileName);
  326. out = new FileOutputStream(filePath + fileName);
  327. wb.write(out);
  328. } catch (FileNotFoundException e) {
  329. e.printStackTrace();
  330. log.error(e.getMessage());
  331. } catch (IOException e) {
  332. e.printStackTrace();
  333. log.error(e.getMessage());
  334. } finally {
  335. try {
  336. if (out != null)
  337. out.close();
  338. } catch (IOException e) {
  339. e.printStackTrace();
  340. }
  341. }
  342. return result;// 返回下载结果
  343. }
  344. //解决合并单元格边框显示不完整的问题
  345. public static void setBorderStyle(int border, CellRangeAddress region, XSSFSheet sheet, XSSFWorkbook wb){
  346. RegionUtil.setBorderBottom(border, region, sheet, wb); //下边框
  347. RegionUtil.setBorderLeft(border, region, sheet, wb); //左边框
  348. RegionUtil.setBorderRight(border, region, sheet, wb); //右边框
  349. RegionUtil.setBorderTop(border, region, sheet, wb); //上边框
  350. }
  351. }

基础类

  1. package com.cyl.util;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.HashSet;
  12. import java.util.List;
  13. import java.util.StringTokenizer;
  14. import org.apache.log4j.Logger;
  15. /**
  16. * 基础类
  17. *
  18. * @author Qiang1_Zhang
  19. */
  20. public class TUtil {
  21. static Logger log = Logger.getLogger(TUtil.class);
  22. /**
  23. * 日期转换函数
  24. *
  25. * @param format
  26. * 需要转换的格式
  27. * @return 转换后的日期
  28. */
  29. public static String format(String format) {
  30. return new SimpleDateFormat(format).format(new Date());
  31. }
  32. /**
  33. * 日期转换函数
  34. *
  35. * @param format
  36. * 需要转换的格式
  37. * @return 转换后的日期
  38. */
  39. public static String format(Date date, String format) {
  40. return new SimpleDateFormat(format).format(date);
  41. }
  42. /**
  43. * 打印函数
  44. *
  45. * @param str
  46. * 对象类型
  47. */
  48. public static void print(Object str) {
  49. System.out.println(str);
  50. }
  51. /**
  52. * 计算距今指定天数的日期
  53. *
  54. * @param day
  55. * 相差的天数,可为负数
  56. * @return 计算之后的日期
  57. */
  58. public static String GetDay(int day) {
  59. Calendar cal = Calendar.getInstance();
  60. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  61. cal.setTime(new Date());// 设置日历时间
  62. cal.add(Calendar.DAY_OF_MONTH, day);// 天数
  63. String strDate = sdf.format(cal.getTime());// 得到你想要的天数
  64. return strDate;
  65. }
  66. /**
  67. * 获取报表模板路径
  68. *
  69. * @return
  70. */
  71. public static String getURL() {
  72. String dir = System.getProperty("user.dir");
  73. print("dir=" + dir);
  74. dir = dir.substring(0, dir.lastIndexOf("\\"));
  75. String filePath = dir;
  76. return filePath;
  77. }
  78. /**
  79. * String类型日期转换为长整型
  80. *
  81. * @param date
  82. * String类型日期
  83. * @param format
  84. * 日期格式
  85. * @return long
  86. */
  87. public static long strDateToLong(String date, String... format) {
  88. String format1 = null;
  89. if (format.length != 0) {
  90. format1 = format[0];
  91. } else {
  92. format1 = "yyyy-MM-dd HH:mm:ss";
  93. }
  94. String sDt = date;
  95. SimpleDateFormat sdf = new SimpleDateFormat(format1);
  96. long lTime = 0;
  97. try {
  98. Date dt2 = sdf.parse(sDt);
  99. lTime = dt2.getTime();
  100. print(lTime);
  101. } catch (ParseException e) {
  102. e.printStackTrace();
  103. }
  104. return lTime;
  105. }
  106. public static void longToString(long l) {
  107. format("");
  108. }
  109. /**
  110. * 获取文件创建时间
  111. *
  112. * @param file
  113. * 文件目录
  114. */
  115. public static String getCreateTime(File file) {
  116. // file = new File("e:/1.xls");
  117. String date = "";
  118. // file.lastModified();
  119. try {
  120. Process process = Runtime.getRuntime().exec(
  121. "cmd.exe /c dir " + file.getAbsolutePath() + "/tc");
  122. InputStream is = process.getInputStream();
  123. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  124. for (int i = 0; i < 5; i++) {// 前五行是其他的信息
  125. br.readLine();
  126. }
  127. String createDateLine = br.readLine();
  128. StringTokenizer tokenizer = new StringTokenizer(createDateLine);
  129. date = tokenizer.nextToken() + " " + tokenizer.nextToken();
  130. br.close();
  131. // print(date);
  132. } catch (IOException e) {
  133. log.error("" + e.getMessage());
  134. }
  135. return date;
  136. }
  137. /**
  138. * 获取文件最后修改时间
  139. *
  140. * @param filePath
  141. * 文件目录
  142. */
  143. public static void getLastModifyTime(File filePath) {
  144. filePath = new File(
  145. "\\\\10.131.18.8\\rt3生產機種\\ProductionReprot\\TraceAlterReprot-reprot");
  146. File[] list = filePath.listFiles();
  147. // for(File file : list){
  148. // print(file.getAbsolutePath()+"\tcreate time:"+getCreateTime(file));
  149. // }
  150. for (File file : list) {
  151. Date date = new Date(file.lastModified());
  152. print(format(date, "yyyy-MM-dd"));
  153. }
  154. }
  155. public static void getFile() {
  156. String root = "\\\\10.131.18.8\\rt3生產機種\\ProductionReprot";
  157. File filePath = new File(root);
  158. File[] list = filePath.listFiles();
  159. for (File file : list) {
  160. print(file.getName()
  161. + "\t"
  162. + new File(file.getAbsolutePath() + "\\"
  163. + TUtil.format("yyyy-MM-dd") + ".xls").exists());
  164. }
  165. }
  166. static void test() {
  167. String today = TUtil.format("yyyy-MM-dd");
  168. String dest = ReadProperties.ReadProprety("server.report.path")
  169. + "TraceAlterReprot-reprot" + "\\" + today + "\\";
  170. print(dest);
  171. File dir = new File(dest);// 创建当天目录
  172. if (!dir.exists()) {
  173. dir.mkdir();
  174. }
  175. }
  176. public static void getTimeDifference() {
  177. try {
  178. Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse("2014-09-15");
  179. Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse("2014-09-14");
  180. print((d2.getTime() - d1.getTime()) / 1000 / 60 / 60 / 24);
  181. } catch (ParseException e) {
  182. e.printStackTrace();
  183. }
  184. }
  185. public List<String> distinctList(List<String> list) {
  186. HashSet<String> h = new HashSet<String>(list);
  187. list.clear();
  188. list.addAll(h);
  189. return list;
  190. }
  191. public List<Object> removeDuplicate(List<Object> list) {
  192. HashSet<Object> h = new HashSet<Object>(list);
  193. list.clear();
  194. list.addAll(h);
  195. return list;
  196. }
  197. /**
  198. * 获取四舍五入的整数
  199. * @param input 乘数
  200. * @param rate 比率
  201. * @return 取整后的结果
  202. */
  203. public double getRound(int input,double rate){
  204. double tmp = input * rate;
  205. return Math.round(tmp);
  206. }
  207. /**
  208. * 获取四舍五入的整数
  209. * @param input 乘数
  210. * @param rate 比率
  211. * @return 取整后的结果
  212. */
  213. public double ceil(int input,double rate){
  214. double tmp = input * rate;
  215. return Math.ceil(tmp);
  216. }
  217. }

前端

  1. /* 我这里是动态拼接的,摘取下载按钮那一条,仅供参考 */
  2. ...省略
  3. str +=  "<a class='btt btn-link' data-title='详细信息' onclick = 'excleDownload("+ data[i].id +")' href='javascript:;'><i class='Hui-iconfont'>&#xe641;</i> 下载</a>";
  4. ...省略
  5. //方法
  6. //下载文件
  7. function excleDownload(id) {
  8. window.location.href = "<%=basePath%>inspection/excleDownload.cyl?id="+ id;
  9. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/842504
推荐阅读
相关标签
  

闽ICP备14008679号