Browse Source

导出报警日志

master
liu_li 3 years ago
parent
commit
20bb1f97cf
  1. 9
      archives/src/main/java/com/storeroom/modules/common/ExcelUtil.java
  2. 25
      common/src/main/java/com/storeroom/utils/CloseUtil.java
  3. 69
      common/src/main/java/com/storeroom/utils/FileUtil.java
  4. 6
      pom.xml
  5. 2
      storeroom/src/main/java/com/storeroom/modules/storeroom3d/service/impl/AlarmInfoServiceImpl.java

9
archives/src/main/java/com/storeroom/modules/common/ExcelUtil.java

@ -14,6 +14,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Timestamp;
@ -90,13 +91,13 @@ public class ExcelUtil {
// 设置表头字体格式
XSSFFont headersFont = workbook.createFont();
headersFont.setColor(new XSSFColor(java.awt.Color.DARK_GRAY));
headersFont.setColor(new XSSFColor((IndexedColorMap) Color.DARK_GRAY));
headersFont.setFontHeightInPoints((short) 14);
headersFont.setBold(true);
// 设置正文字体格式
XSSFFont dataSetFont = workbook.createFont();
dataSetFont.setColor(new XSSFColor(java.awt.Color.BLACK));
dataSetFont.setColor(new XSSFColor((IndexedColorMap) Color.BLACK));
dataSetFont.setBold(false);
// 创建表头样式
@ -227,13 +228,13 @@ public class ExcelUtil {
// 设置表头字体格式
XSSFFont headersFont = workbook.createFont();
headersFont.setColor(new XSSFColor(java.awt.Color.DARK_GRAY));
headersFont.setColor(new XSSFColor((IndexedColorMap) Color.DARK_GRAY));
headersFont.setFontHeightInPoints((short) 14);
headersFont.setBold(true);
// 设置正文字体格式
XSSFFont dataSetFont = workbook.createFont();
dataSetFont.setColor(new XSSFColor(java.awt.Color.BLACK));
dataSetFont.setColor(new XSSFColor((IndexedColorMap) Color.BLACK));
dataSetFont.setBold(false);
// 创建表头样式

25
common/src/main/java/com/storeroom/utils/CloseUtil.java

@ -0,0 +1,25 @@
package com.storeroom.utils;
import java.io.Closeable;
public class CloseUtil {
public static void close(Closeable closeable) {
if (null != closeable) {
try {
closeable.close();
} catch (Exception e) {
// 静默关闭
}
}
}
public static void close(AutoCloseable closeable) {
if (null != closeable) {
try {
closeable.close();
} catch (Exception e) {
// 静默关闭
}
}
}
}

69
common/src/main/java/com/storeroom/utils/FileUtil.java

@ -77,7 +77,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{
File file = null;
try {
// 用uuid作为文件名防止生成的临时文件重复
file = File.createTempFile(IdUtil.simpleUUID(), prefix);
file = new File(SYS_TEM_DIR + IdUtil.simpleUUID() + prefix);
// MultipartFile to File
multipartFile.transferTo(file);
} catch (IOException e) {
@ -135,20 +135,26 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{
/**
* inputStream File
*/
static File inputStreamToFile(InputStream ins, String name) throws Exception {
static File inputStreamToFile(InputStream ins, String name){
File file = new File(SYS_TEM_DIR + name);
if (file.exists()) {
return file;
}
OutputStream os = new FileOutputStream(file);
int bytesRead;
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
OutputStream os = null;
try {
os = new FileOutputStream(file);
int bytesRead;
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
CloseUtil.close(os);
CloseUtil.close(ins);
}
os.close();
ins.close();
return file;
}
@ -195,8 +201,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{
sheet.trackAllColumnsForAutoSizing();
//列宽自适应
writer.autoSizeColumnAll();
//列宽自适应支持中文单元格
sizeChineseColumn(sheet, writer);
//response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//test.xls是弹出下载对话框的文件名不能为中文中文请自行编码
@ -209,33 +213,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{
IoUtil.close(out);
}
/**
* 自适应宽度(中文支持)
*/
private static void sizeChineseColumn(SXSSFSheet sheet, BigExcelWriter writer) {
for (int columnNum = 0; columnNum < writer.getColumnCount(); columnNum++) {
int columnWidth = sheet.getColumnWidth(columnNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
SXSSFRow currentRow;
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(columnNum) != null) {
SXSSFCell currentCell = currentRow.getCell(columnNum);
if (currentCell.getCellTypeEnum() == CellType.STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
sheet.setColumnWidth(columnNum, columnWidth * 256);
}
}
public static String getFileType(String type) {
String documents = "txt doc pdf ppt pps xlsx xls docx";
String music = "mp3 wav wma mpa ram ra aac aif m4a";
@ -258,7 +235,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{
// 1M
int len = 1024 * 1024;
if (size > (maxSize * len)) {
throw new BaseException("文件超出规定大小");
throw new BaseException("文件超出规定大小:" + maxSize + "MB");
}
}
@ -268,7 +245,10 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{
public static boolean check(File file1, File file2) {
String img1Md5 = getMd5(file1);
String img2Md5 = getMd5(file2);
return img1Md5.equals(img2Md5);
if(img1Md5 != null){
return img1Md5.equals(img2Md5);
}
return false;
}
/**
@ -281,16 +261,19 @@ public class FileUtil extends cn.hutool.core.io.FileUtil{
private static byte[] getByte(File file) {
// 得到文件长度
byte[] b = new byte[(int) file.length()];
InputStream in = null;
try {
InputStream in = new FileInputStream(file);
in = new FileInputStream(file);
try {
System.out.println(in.read(b));
} catch (IOException e) {
log.error(e.getMessage(), e);
}
} catch (FileNotFoundException e) {
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
} finally {
CloseUtil.close(in);
}
return b;
}

6
pom.xml

@ -164,17 +164,17 @@
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.12.0</version>
<version>2.12.2</version>
</dependency>
<!-- fastjson -->

2
storeroom/src/main/java/com/storeroom/modules/storeroom3d/service/impl/AlarmInfoServiceImpl.java

@ -192,7 +192,7 @@ public class AlarmInfoServiceImpl implements AlarmInfoService {
@Override
public List<AlarmLog> queryAll(AlarmLogCriteria criteria) {
return null;
return alarmLogRepository.findAll((root, query, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
}
/**

Loading…
Cancel
Save