![在这里插入图片描述](https://img-blog.csdnimg.cn/dba89d7df02f487590c06417ddbe165d.png#pic_center)
压缩包使用说明
public class ZipFileUtils {
/**
* web下载打成压缩包的文件--流方式
*
* @param response 响应
* @param fileList 文件列表
* @param zipName 压缩包名
*/
public static void downloadZipFiles(HttpServletResponse response, List<File> fileList, String zipName) {
ZipOutputStream zipOutputStream = null;
try {
//设置响应头
response.reset();
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
//设置文件名称
response.setHeader("Content-Disposition", "attachment;filename=" + zipName);
zipOutputStream = new ZipOutputStream(response.getOutputStream());
for (File file : fileList) {
toZip(zipOutputStream,file);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
//关闭资源
if (null != zipOutputStream) {
try {
zipOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 压缩文件
*
* @param zipOutputStream 压缩文件流
* @param file 待压缩文件
*/
private static void toZip(ZipOutputStream zipOutputStream, File file) {
String filename = file.getName();
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(Files.newInputStream(file.toPath()));
//设置压缩包内文件的名称
zipOutputStream.putNextEntry(new ZipEntry(filename));
int size;
byte[] buffer = new byte[4096];
while ((size = bis.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, size);
}
zipOutputStream.closeEntry();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
//关闭资源
if (null != bis) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class ExcelWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
// 统计setColumnWidth被调用多少次
private static int count = 0;
@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head,
Integer relativeRowIndex, Boolean isHead) {
// 简单设置
Sheet sheet = writeSheetHolder.getSheet();
sheet.setColumnWidth(cell.getColumnIndex(), 5000);
// System.out.println("0000000"+count++);
}
//设置excel样式
public static HorizontalCellStyleStrategy getStyleStrategy() {
// 头的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 背景设置为灰色 浅蓝色
headWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 20);
//行宽
// 字体样式
headWriteFont.setFontName("Frozen");
headWriteCellStyle.setWriteFont(headWriteFont);
//自动换行
headWriteCellStyle.setWrapped(false);
// 水平对齐方式
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 垂直对齐方式
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//锁定表头
headWriteCellStyle.setLocked(true);
// 内容的策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
// contentWriteCellStyle.setFillPatternType(FillPatternType.SQUARES);
// 背景白色
contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
WriteFont contentWriteFont = new WriteFont();
// 字体大小
contentWriteFont.setFontHeightInPoints((short) 12);
// 字体样式
contentWriteFont.setFontName("Song");
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
}
}