@@ -13,6 +13,10 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.klp.common.exception.ServiceException ;
import com.klp.common.utils.StringUtils ;
import lombok.RequiredArgsConstructor ;
import org.apache.poi.ss.usermodel.* ;
import org.apache.poi.ss.util.CellRangeAddress ;
import org.apache.poi.xssf.usermodel.XSSFFont ;
import org.apache.poi.xssf.usermodel.XSSFWorkbook ;
import org.springframework.stereotype.Service ;
import com.klp.aps.domain.bo.ApsPlanSheetBo ;
import com.klp.aps.domain.vo.ApsPlanSheetVo ;
@@ -21,6 +25,11 @@ import com.klp.aps.mapper.ApsPlanSheetMapper;
import com.klp.aps.service.IApsPlanSheetService ;
import com.klp.aps.service.IApsPlanDetailService ;
import javax.servlet.ServletOutputStream ;
import javax.servlet.http.HttpServletResponse ;
import java.io.IOException ;
import java.net.URLEncoder ;
import java.nio.charset.StandardCharsets ;
import java.util.List ;
import java.util.Map ;
import java.util.Collection ;
@@ -42,7 +51,7 @@ public class ApsPlanSheetServiceImpl implements IApsPlanSheetService {
* 查询排产单主
*/
@Override
public ApsPlanSheetVo queryById ( Long planSheetId ) {
public ApsPlanSheetVo queryById ( Long planSheetId ) {
return baseMapper . selectVoById ( planSheetId ) ;
}
@@ -104,7 +113,7 @@ public class ApsPlanSheetServiceImpl implements IApsPlanSheetService {
/**
* 保存前的数据校验
*/
private void validEntityBeforeSave ( ApsPlanSheet entity ) {
private void validEntityBeforeSave ( ApsPlanSheet entity ) {
//TODO 做一些数据校验,如唯一约束
}
@@ -113,7 +122,7 @@ public class ApsPlanSheetServiceImpl implements IApsPlanSheetService {
*/
@Override
public Boolean deleteWithValidByIds ( Collection < Long > ids , Boolean isValid ) {
if ( isValid ) {
if ( isValid ) {
//TODO 做一些业务上的校验,判断是否需要校验
}
planDetailService . deleteByPlanSheetIds ( ids ) ;
@@ -121,22 +130,28 @@ public class ApsPlanSheetServiceImpl implements IApsPlanSheetService {
}
@Override
public void exportExcel ( ApsPlanSheetQueryReq req , javax . servlet . http . HttpServletResponse response ) {
public void exportExcel ( ApsPlanSheetQueryReq req , HttpServletResponse response ) {
List < ApsPlanSheetRowVo > rows = queryListAll ( req ) ;
try ( org . apache . poi . ss . usermodel . Workbook wb = new org . apache . poi . xssf . usermodel . XSSFWorkbook( ) ) {
org . apache . poi . ss . usermodel . Sheet sheet = wb . createSheet ( " 快速排产表 " ) ;
try ( Workbook wb = new XSSFWorkbook( ) ) {
Sheet sheet = wb . createSheet ( " 快速排产表 " ) ;
int r = 0 ;
org . apache . poi . ss . usermodel . Row title = sheet . createRow ( r + + ) ;
Row title = sheet . createRow ( r + + ) ;
title . setHeightInPoints ( 36f ) ;
org . apache . poi . ss . usermodel . Cell t0 = title . createCell ( 0 ) ;
t0 . s etCellValue ( " 快速排产表 " ) ;
sheet . addMergedRegion ( new org . apache . poi . ss . util . CellRangeAddress ( 0 , 0 , 0 , 38 ) ) ;
Cell t0 = title . createCell ( 0 ) ;
Long planSheetId = req . g etPlanSheetId ( ) ;
if ( planSheetId ! = null ) {
ApsPlanSheetVo planSheet = queryById ( planSheetId ) ;
t0 . setCellValue ( planSheet . getPlanDate ( ) . toString ( ) + " ———— " + planSheet . getLineName ( ) + " ———— " + planSheet . getPlanCode ( ) ) ;
} else {
t0 . setCellValue ( " 快速排产表 " ) ;
}
sheet . addMergedRegion ( new CellRangeAddress ( 0 , 0 , 0 , 38 ) ) ;
org . apache . poi . ss . usermodel . CellStyle titleStyle = wb . createCellStyle ( ) ;
titleStyle . setAlignment ( org . apache . poi . ss . usermodel . HorizontalAlignment. CENTER ) ;
titleStyle . setVerticalAlignment ( org . apache . poi . ss . usermodel . VerticalAlignment. CENTER ) ;
org . apache . poi . xssf . usermodel . XSSFFont titleFont = ( ( org . apache . poi . xssf . usermodel . XSSFWorkbook) wb ) . createFont ( ) ;
CellStyle titleStyle = wb . createCellStyle ( ) ;
titleStyle . setAlignment ( HorizontalAlignment . CENTER ) ;
titleStyle . setVerticalAlignment ( VerticalAlignment . CENTER ) ;
XSSFFont titleFont = ( ( XSSFWorkbook) wb ) . createFont ( ) ;
titleFont . setBold ( true ) ;
titleFont . setFontHeightInPoints ( ( short ) 15 ) ;
titleStyle . setFont ( titleFont ) ;
@@ -150,15 +165,50 @@ public class ApsPlanSheetServiceImpl implements IApsPlanSheetService {
" 厂家 " , " 原料信息 " , " 原料厚度mm " , " 宽度mm " , " 原料钢卷 " , " 原料卷号 " , " 钢卷位置 " , " 包装要求 " , " 镀层种类 " , " 原料净重 " ,
" 开始时间 " , " 结束时间 " , " 明细备注 "
} ;
// ===================== 【绝对不报错】表头样式 =====================
org . apache . poi . ss . usermodel . CellStyle headStyle = wb . createCellStyle ( ) ;
org . apache . poi . ss . usermodel . Font headFont = wb . createFont ( ) ;
// 1. 字体加粗
headFont . setBold ( true ) ;
headStyle . setFont ( headFont ) ;
// 2. 居中
headStyle . setAlignment ( org . apache . poi . ss . usermodel . HorizontalAlignment . CENTER ) ;
headStyle . setVerticalAlignment ( org . apache . poi . ss . usermodel . VerticalAlignment . CENTER ) ;
// 3. 背景颜色(浅蓝色)
headStyle . setFillPattern ( org . apache . poi . ss . usermodel . FillPatternType . SOLID_FOREGROUND ) ;
headStyle . setFillForegroundColor ( org . apache . poi . ss . usermodel . IndexedColors . PALE_BLUE . getIndex ( ) ) ;
// 4. 自动换行
headStyle . setWrapText ( true ) ;
// 创建表头行
org . apache . poi . ss . usermodel . Row head = sheet . createRow ( r + + ) ;
head . setHeightInPoints ( 24 ) ;
// 设置表头内容 + 样式
for ( int i = 0 ; i < headers . length ; i + + ) {
head . createCell ( i ) . setCellValue ( headers [ i ] ) ;
org . apache . poi . ss . u sermodel . Cell cell = head . createCell ( i ) ;
cell . setCellValue ( headers [ i ] ) ;
cell . setCellStyle ( headStyle ) ;
}
if ( rows ! = null ) {
// ================== 【统一样式: 前7列居中】 ==================
CellStyle centerStyle = wb . createCellStyle ( ) ;
centerStyle . setAlignment ( HorizontalAlignment . CENTER ) ;
centerStyle . setVerticalAlignment ( VerticalAlignment . CENTER ) ;
// ================== 数据行 ==================
int dataStartRow = r ;
if ( rows ! = null & & ! rows . isEmpty ( ) ) {
for ( ApsPlanSheetRowVo row : rows ) {
org . apache . poi . ss . usermodel . Row rr = sheet . createRow ( r + + ) ;
Row rr = sheet . createRow ( r + + ) ;
int cc = 0 ;
// ====== 前7列: 相同内容, 需要居中 + 后续合并 ======
rr . createCell ( cc + + ) . setCellValue ( nvl ( row . getLineName ( ) , row . getLineId ( ) ) ) ;
rr . createCell ( cc + + ) . setCellValue ( row . getPlanDate ( ) = = null ? " " : row . getPlanDate ( ) . toString ( ) ) ;
rr . createCell ( cc + + ) . setCellValue ( nvl ( row . getPlanCode ( ) , " " ) ) ;
@@ -167,6 +217,12 @@ public class ApsPlanSheetServiceImpl implements IApsPlanSheetService {
rr . createCell ( cc + + ) . setCellValue ( nvl ( row . getUpdateBy ( ) , " " ) ) ;
rr . createCell ( cc + + ) . setCellValue ( nvl ( row . getMasterRemark ( ) , " " ) ) ;
// 前7列设置居中
for ( int i = 0 ; i < 7 ; i + + ) {
rr . getCell ( i ) . setCellStyle ( centerStyle ) ;
}
// ====== 后面列不变 ======
rr . createCell ( cc + + ) . setCellValue ( nvl ( row . getBizSeqNo ( ) , " " ) ) ;
rr . createCell ( cc + + ) . setCellValue ( nvl ( row . getOrderCode ( ) , " " ) ) ;
rr . createCell ( cc + + ) . setCellValue ( nvl ( row . getContractCode ( ) , " " ) ) ;
@@ -207,6 +263,21 @@ public class ApsPlanSheetServiceImpl implements IApsPlanSheetService {
}
}
// ================== 【核心: 前7列自动合并】 ==================
int dataEndRow = r - 1 ;
if ( dataStartRow < = dataEndRow ) {
for ( int col = 0 ; col < 7 ; col + + ) { // 0~6 共7列
CellRangeAddress region = new CellRangeAddress (
dataStartRow ,
dataEndRow ,
col ,
col
) ;
sheet . addMergedRegion ( region ) ;
}
}
// 自适应列宽
for ( int i = 0 ; i < headers . length ; i + + ) {
sheet . autoSizeColumn ( i , true ) ;
int w = sheet . getColumnWidth ( i ) ;
@@ -214,16 +285,16 @@ public class ApsPlanSheetServiceImpl implements IApsPlanSheetService {
}
String filename = " aps_quick_sheet_ " + System . currentTimeMillis ( ) + " .xlsx " ;
String encoded = java . net . URLEncoder. encode ( filename , java . nio . charset . StandardCharsets. UTF_8 . name ( ) ) ;
response . setCharacterEncoding ( java . nio . charset . StandardCharsets. UTF_8 . name ( ) ) ;
String encoded = URLEncoder . encode ( filename , StandardCharsets . UTF_8 . name ( ) ) ;
response . setCharacterEncoding ( StandardCharsets . UTF_8 . name ( ) ) ;
response . setContentType ( " application/vnd.openxmlformats-officedocument.spreadsheetml.sheet " ) ;
response . setHeader ( " Content-Disposition " , " attachment; filename*=UTF-8'' " + encoded ) ;
try ( javax . servlet . ServletOutputStream os = response . getOutputStream ( ) ) {
try ( ServletOutputStream os = response . getOutputStream ( ) ) {
wb . write ( os ) ;
os . flush ( ) ;
}
} catch ( java . io . IOException e ) {
} catch ( IOException e ) {
throw new ServiceException ( " 导出失败: " + e . getMessage ( ) ) ;
}
}