修改bug

This commit is contained in:
yangjun 2026-03-26 10:49:51 +08:00
parent 07e95dd1db
commit 25d3c4c216
3 changed files with 222 additions and 29 deletions

View File

@ -155,7 +155,7 @@ public class NuBizAllMaterialInfoController extends JeecgController<NuBizAllMate
@GetMapping("/exportXls")
public void exportXls(NuBizAllMaterialInfo nuBizAllMaterialInfo,HttpServletResponse response) {
public void exportXls(NuBizAllMaterialInfo nuBizAllMaterialInfo,HttpServletResponse response) throws IOException {
nuBizAllMaterialInfoService.exportExcel(nuBizAllMaterialInfo,response);
}

View File

@ -4,6 +4,7 @@ import com.nu.modules.allMaterialInfo.entity.NuBizAllMaterialInfo;
import com.baomidou.mybatisplus.extension.service.IService;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Description: 物料信息汇总
@ -13,5 +14,5 @@ import javax.servlet.http.HttpServletResponse;
*/
public interface INuBizAllMaterialInfoService extends IService<NuBizAllMaterialInfo> {
void exportExcel(NuBizAllMaterialInfo nuBizAllMaterialInfo,HttpServletResponse response);
void exportExcel(NuBizAllMaterialInfo nuBizAllMaterialInfo,HttpServletResponse response) throws IOException;
}

View File

@ -1,5 +1,6 @@
package com.nu.modules.allMaterialInfo.service.impl;
import com.aliyun.core.utils.IOUtils;
import com.nu.modules.allMaterialInfo.entity.NuBizAllMaterialInfo;
import com.nu.modules.allMaterialInfo.mapper.NuBizAllMaterialInfoMapper;
import com.nu.modules.allMaterialInfo.service.INuBizAllMaterialInfoService;
@ -23,7 +24,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
@ -37,7 +43,7 @@ import java.util.List;
public class NuBizAllMaterialInfoServiceImpl extends ServiceImpl<NuBizAllMaterialInfoMapper, NuBizAllMaterialInfo> implements INuBizAllMaterialInfoService {
@Override
public void exportExcel(NuBizAllMaterialInfo nuBizAllMaterialInfo,HttpServletResponse response) {
public void exportExcel(NuBizAllMaterialInfo nuBizAllMaterialInfo,HttpServletResponse response) throws IOException {
// 1. 查询数据
QueryWrapper<NuBizAllMaterialInfo> queryWrapper = new QueryWrapper<>();
List<NuBizAllMaterialInfo> dataList = baseMapper.selectList(queryWrapper);
@ -82,37 +88,128 @@ public class NuBizAllMaterialInfoServiceImpl extends ServiceImpl<NuBizAllMateria
// 创建可编辑样式
CellStyle unlockedStyleRed = workbook.createCellStyle();
unlockedStyleRed.setLocked(false);
// unlockedStyleRed.setBorderBottom(BorderStyle.THIN);
// unlockedStyleRed.setBorderTop(BorderStyle.THIN);
// unlockedStyleRed.setBorderLeft(BorderStyle.THIN);
// unlockedStyleRed.setBorderRight(BorderStyle.THIN);
// unlockedStyleRed.setAlignment(HorizontalAlignment.CENTER);
// unlockedStyleRed.setVerticalAlignment(VerticalAlignment.CENTER);
// 创建红色字体
Font redFont = workbook.createFont();
redFont.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
// 可选设置字体大小加粗等
// redFont.setFontHeightInPoints((short) 12);
// redFont.setBold(true);
// 将红色字体应用到样式
unlockedStyleRed.setFont(redFont);
// 6. 创建大标题行第1行
Row titleOneRow = sheet.createRow(0);
Cell materialTitleCellRow = titleOneRow.createCell(0);
materialTitleCellRow.setCellValue("供应商物料列表");
materialTitleCellRow.setCellStyle(lockedStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6)); // 合并 B1:G1
// 创建加粗字体
Font boldFont = workbook.createFont();
boldFont.setBold(true);
//创建加粗样式
CellStyle boldUnlockedStyle = workbook.createCellStyle();
boldUnlockedStyle.cloneStyleFrom(unlockedStyle); // 从原有样式克隆
boldUnlockedStyle.setFont(boldFont); // 应用加粗字体
boldUnlockedStyle.setLocked(false);
boldUnlockedStyle.setBorderBottom(BorderStyle.THIN);
boldUnlockedStyle.setBorderTop(BorderStyle.THIN);
boldUnlockedStyle.setBorderLeft(BorderStyle.THIN);
boldUnlockedStyle.setBorderRight(BorderStyle.THIN);
Font boldFont3 = workbook.createFont();
boldFont3.setBold(true);
boldFont3.setFontHeightInPoints((short) 20);
//创建加粗样式字体放大
CellStyle boldUnlockedStyle2 = workbook.createCellStyle();
boldUnlockedStyle2.cloneStyleFrom(unlockedStyle); // 从原有样式克隆
boldUnlockedStyle2.setFont(boldFont3); // 应用加粗字体
boldUnlockedStyle2.setLocked(false);
boldUnlockedStyle2.setBorderBottom(BorderStyle.THIN);
boldUnlockedStyle2.setBorderTop(BorderStyle.THIN);
boldUnlockedStyle2.setBorderLeft(BorderStyle.THIN);
boldUnlockedStyle2.setBorderRight(BorderStyle.THIN);
//------------------------插入第一行图片----------------
// 创建行和合并单元格
Row titleOneRow = sheet.createRow(0);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1)); // 合并A1:B1
//创建单元格不设置文字因为要插入图片
Cell materialTitleCellRow = titleOneRow.createCell(0);
materialTitleCellRow.setCellStyle(unlockedStyle);
titleOneRow.setHeightInPoints(60.0f);
//调用插入图片方法
insertImageToCell(sheet, workbook, 0, 0, 2, 1, "https://www.focusnu.com/media/directive/showLogo.png");
//------------------------插入第一行图片----------------
//------------------------插入第一行文字----------------
Cell materialTitleCellRow2 = titleOneRow.createCell(2);
materialTitleCellRow2.setCellValue("XX机构物料合作订购表");
materialTitleCellRow2.setCellStyle(boldUnlockedStyle2);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 6)); // 合并
//------------------------插入第一行文字----------------
//------------------------插入第一行说明文字----------------
// 添加说明文字
CellStyle unlockedStyleShuoming = workbook.createCellStyle();
unlockedStyleShuoming.setLocked(false);
unlockedStyleShuoming.setBorderBottom(BorderStyle.THIN);
unlockedStyleShuoming.setBorderTop(BorderStyle.THIN);
unlockedStyleShuoming.setBorderLeft(BorderStyle.THIN);
unlockedStyleShuoming.setBorderRight(BorderStyle.THIN);
unlockedStyleShuoming.setVerticalAlignment(VerticalAlignment.TOP);
// 创建红色字体
Font redFont2 = workbook.createFont();
redFont2.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
unlockedStyleShuoming.setFont(redFont2);
Cell materialTitleCellRow3 = titleOneRow.createCell(7);
// 创建普通字体
Font normalFont = workbook.createFont();
normalFont.setColor(IndexedColors.RED.getIndex());
// 创建富文本
RichTextString richText = workbook.getCreationHelper().createRichTextString(
"模板导入说明:\n" +
" 1、A列选择 \"\":表示该供应商可提供此货品;选择 \"\":表示该供应商不供应此货品。\n" +
" 2、B列、C列、D列、E列为系统预设基础数据不可编辑修改用于统一物料标准。\n" +
" 3、F列、G列a、销售单价请填写数字保留 2 位小数单位b、销售单位请填写实际供货包装单位\"包、箱、支、片\" 等,确保与实际供货包装一致。\n" +
" 4、模板保存模板文件请以 \"供应商名称 + 物料列表\" 的格式命名保存。"
);
Font boldFont2 = workbook.createFont();
boldFont2.setBold(true);
boldFont2.setColor(IndexedColors.RED.getIndex());
// 只设置"模板导入说明:"为加粗
richText.applyFont(0, 6, boldFont2); // "模板导入说明:"的前6个字符加粗
richText.applyFont(6, richText.length(), normalFont); // 其余文字普通字体
materialTitleCellRow3.setCellValue(richText);
// 创建或获取一个样式并设置自动换行
CellStyle wrappedStyle = workbook.createCellStyle();
wrappedStyle.cloneStyleFrom(unlockedStyleShuoming); // 从你原有样式克隆
wrappedStyle.setWrapText(true); // 关键设置自动换行
// 将样式应用到单元格
materialTitleCellRow3.setCellStyle(wrappedStyle);
// 合并区域
sheet.addMergedRegion(new CellRangeAddress(0, 6, 7, 12));
//------------------------插入第一行说明文字----------------
//------------------------插入第二行文字----------------
Row titleRow = sheet.createRow(1);
Cell materialTitleCell = titleRow.createCell(0);
materialTitleCell.setCellStyle(boldUnlockedStyle);
materialTitleCell.setCellValue("供应商名称:");
materialTitleCell.setCellStyle(lockedStyle);
Cell materialTitleCell2 = titleRow.createCell(1);
materialTitleCell2.setCellValue("请填写供应商名称");
materialTitleCell2.setCellStyle(unlockedStyleRed);
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 5)); // 合并 B1:G1
materialTitleCell2.setCellStyle(boldUnlockedStyle);
materialTitleCell2.setCellValue("");
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2)); // 合并
Cell materialTitleCell3 = titleRow.createCell(3);
materialTitleCell3.setCellStyle(boldUnlockedStyle);
materialTitleCell3.setCellValue("联系人");
Cell materialTitleCell4 = titleRow.createCell(4);
materialTitleCell4.setCellStyle(boldUnlockedStyle);
materialTitleCell4.setCellValue("");
Cell materialTitleCell5 = titleRow.createCell(5);
materialTitleCell5.setCellStyle(boldUnlockedStyle);
materialTitleCell5.setCellValue("联系电话");
Cell materialTitleCell6 = titleRow.createCell(6);
materialTitleCell6.setCellStyle(boldUnlockedStyle);
materialTitleCell6.setCellValue("");
titleRow.setHeightInPoints(30.0f);
//------------------------插入第二行文字----------------
// 7. 创建列名行第2行
Row headerRow = sheet.createRow(2);
@ -122,6 +219,7 @@ public class NuBizAllMaterialInfoServiceImpl extends ServiceImpl<NuBizAllMateria
cell.setCellValue(headers[i]);
cell.setCellStyle(lockedStyle);
}
headerRow.setHeightInPoints(20.0f);
// 8. 填充数据从第3行开始
int rowNum = 3;
@ -138,18 +236,17 @@ public class NuBizAllMaterialInfoServiceImpl extends ServiceImpl<NuBizAllMateria
// 第5-6列销售单价销售单位 - 可编辑
setCellValueWithStyle(row, 5, info.getSalesUnitPrice(), unlockedStyle);
setCellValueWithStyle(row, 6, info.getSalesUnit(), unlockedStyle);
// 第7列是否提供 - 可编辑
// setCellValueWithStyle(row, 6, info.getIsSftg(), unlockedStyle);
row.setHeightInPoints(20.0f);
}
// 9. 设置列宽
int[] colWidths = {5000, 10000, 7000, 7000,10000, 5000, 5000};
int[] colWidths = {5000, 5000, 5000, 5000,5000, 5000, 5000};
for (int i = 0; i < colWidths.length; i++) {
sheet.setColumnWidth(i, colWidths[i]);
}
// 10. 冻结前两行大标题 + 列名
sheet.createFreezePane(0, 2, 0, 2);
// sheet.createFreezePane(0, 2, 0, 2);
// 11. 设置是否提供列为下拉选第7列index=6
String[] options = {"", ""};
@ -180,6 +277,101 @@ public class NuBizAllMaterialInfoServiceImpl extends ServiceImpl<NuBizAllMateria
}
}
// 修改插入图片方法支持URL路径
public static void insertImageToCell(Sheet sheet, Workbook workbook,
int startCol, int startRow,
int endCol, int endRow,
String imagePathOrUrl) throws IOException {
byte[] imageBytes;
// 判断是URL还是本地路径
if (imagePathOrUrl.startsWith("http://") || imagePathOrUrl.startsWith("https://")) {
// 处理线上图片URL
imageBytes = downloadImageFromUrl(imagePathOrUrl);
} else {
// 处理本地图片路径
FileInputStream fis = new FileInputStream(imagePathOrUrl);
imageBytes = IOUtils.toByteArray(fis);
fis.close();
}
// 确定图片类型
int pictureType = getPictureType(imagePathOrUrl);
// 添加图片到工作簿
int pictureIdx = workbook.addPicture(imageBytes, pictureType);
// 创建绘图工具
Drawing<?> drawing = sheet.createDrawingPatriarch();
// 创建锚点
ClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
anchor.setCol1(startCol); // 起始列
anchor.setRow1(startRow); // 起始行
anchor.setCol2(endCol); // 结束列不包含
anchor.setRow2(endRow); // 结束行不包含
// 插入图片
Picture picture = drawing.createPicture(anchor, pictureIdx);
}
// 从URL下载图片的方法
private static byte[] downloadImageFromUrl(String imageUrl) throws IOException {
try {
// 使用Java内置的HttpURLConnection
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000); // 10秒连接超时
connection.setReadTimeout(30000); // 30秒读取超时
// 检查响应码
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new IOException("HTTP请求失败响应码: " + responseCode);
}
// 读取图片数据
try (InputStream inputStream = connection.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return outputStream.toByteArray();
}
} catch (Exception e) {
throw new IOException("下载图片失败: " + imageUrl, e);
}
}
// 根据URL或路径确定图片类型
private static int getPictureType(String path) {
String lowerPath = path.toLowerCase();
if (lowerPath.endsWith(".png")) {
return Workbook.PICTURE_TYPE_PNG;
} else if (lowerPath.endsWith(".jpg") || lowerPath.endsWith(".jpeg")) {
return Workbook.PICTURE_TYPE_JPEG;
} else if (lowerPath.endsWith(".bmp") || lowerPath.endsWith(".dib")) {
return Workbook.PICTURE_TYPE_DIB;
} else if (lowerPath.endsWith(".emf")) {
return Workbook.PICTURE_TYPE_EMF;
} else if (lowerPath.endsWith(".wmf")) {
return Workbook.PICTURE_TYPE_WMF;
} else if (lowerPath.endsWith(".gif")) {
return Workbook.PICTURE_TYPE_JPEG; // POI不支持GIF转换为JPEG
} else {
// 默认或从Content-Type判断
return Workbook.PICTURE_TYPE_JPEG;
}
}
/**
* 设置单元格值并应用样式
*/