厦门自动跑批脚本
This commit is contained in:
@@ -597,11 +597,11 @@
|
|||||||
<!-- <version>1.2.83</version>-->
|
<!-- <version>1.2.83</version>-->
|
||||||
<!-- </dependency>-->
|
<!-- </dependency>-->
|
||||||
|
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>mysql</groupId>
|
<!-- <groupId>mysql</groupId>-->
|
||||||
<artifactId>mysql-connector-java</artifactId>
|
<!-- <artifactId>mysql-connector-java</artifactId>-->
|
||||||
<version>8.0.33</version>
|
<!-- <version>8.0.33</version>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>com.fasterxml.jackson.core</groupId>-->
|
<!-- <groupId>com.fasterxml.jackson.core</groupId>-->
|
||||||
<!-- <artifactId>jackson-core</artifactId>-->
|
<!-- <artifactId>jackson-core</artifactId>-->
|
||||||
|
|||||||
+1
-1
@@ -11,7 +11,7 @@ class NanjiangVillage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def run(){
|
def run(){
|
||||||
ExcelUtils.multiThreadingModify("D:\\数据\\南京银行\\县城清洗-结果.xlsx",{d->
|
ExcelUtils.multiThreadingModify("D:\\数据\\南京银行\\新地址核实(县城).xlsx",{d->
|
||||||
|
|
||||||
if(!d['F']){
|
if(!d['F']){
|
||||||
def dto = OpenApiUtils.addrVerifyNanjing(null, d['A'], 2)
|
def dto = OpenApiUtils.addrVerifyNanjing(null, d['A'], 2)
|
||||||
|
|||||||
+2
-2
@@ -10,10 +10,10 @@ class XiamenVillage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def run(){
|
def run(){
|
||||||
ExcelUtils.multiThreadingModify("D:\\数据\\厦门\\涉农组织类型判定(to估家,2025-10-31)-以此为准.xlsx",{ d->
|
ExcelUtils.multiThreadingModify("D:\\数据\\厦门\\工业园区与涉农组织类型矛盾.xlsx",{ d->
|
||||||
|
|
||||||
if(!d['R']){
|
if(!d['R']){
|
||||||
def dto = OpenApiUtils.addrVerifyXiameng(null, d['B'], 2)
|
def dto = OpenApiUtils.addrVerifyXiameng(null, d['A'], 2)
|
||||||
if(dto){
|
if(dto){
|
||||||
// def dto = jsonObj.result
|
// def dto = jsonObj.result
|
||||||
d['F'] = dto.province
|
d['F'] = dto.province
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@ import com.mongodb.DBCollection
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
//@BatchOwner(Watcher.GaoJuan)
|
@BatchOwner(Watcher.GaoJuan)
|
||||||
class RelatedTaiWanBatch extends BaseBatch{
|
class RelatedTaiWanBatch extends BaseBatch{
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+86
@@ -0,0 +1,86 @@
|
|||||||
|
package com.fangjia.script.gj.xiamen
|
||||||
|
|
||||||
|
import com.fangjia.script.BaseBatch
|
||||||
|
import com.fangjia.script.gj.OpenApiUtils
|
||||||
|
import com.fangjia.script.util.DateUtil
|
||||||
|
import com.fangjia.script.util.ExcelUtils
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 厦门每月月初自动跑涉农数据
|
||||||
|
*/
|
||||||
|
class XiaMenVillageBatch extends BaseBatch{
|
||||||
|
|
||||||
|
|
||||||
|
static main(args) {
|
||||||
|
runBatch(new XiaMenVillageBatch(), args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void process(Object args) {
|
||||||
|
def monthStr = DateUtil.formatByPattern(new Date(), "yyyy-MM");
|
||||||
|
def outFileName = "xmbank_"+monthStr+"_result.xlsx"
|
||||||
|
def inFileName = "xmbank_"+monthStr+".xlsx"
|
||||||
|
def exists = checkFileExists(outFileName)
|
||||||
|
if(!exists){
|
||||||
|
def rows = []
|
||||||
|
rows << ["序号","地址","判定后涉农组织类型(城市企业[不含县城企业]、农村企业、县城企业)","所属工业园区或经济开发区"]
|
||||||
|
def rets = XmBankVillageFtpUtils.readExcelFile(inFileName)
|
||||||
|
if(rets){
|
||||||
|
for(def row : rets){
|
||||||
|
if(row[1]){
|
||||||
|
def dto = OpenApiUtils.addrVerifyXiameng(null, row[1], 2)
|
||||||
|
if(dto){
|
||||||
|
row[2] = dto.addrArea //所属区域 农村区域 城市区域 县城城区(对公)
|
||||||
|
if(dto.devDistrictName){
|
||||||
|
row[3] = dto.devDistrictName//开发区
|
||||||
|
}else{
|
||||||
|
row[3] = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rows << row
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uploadExcel(rows, outFileName)
|
||||||
|
|
||||||
|
}else{
|
||||||
|
println "已存在 "+ DateUtil.formatByPattern(new Date(), "yyyy-MM-dd HH:mm:ss")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static boolean uploadExcel(def data, String fileName, String sheetName = "Sheet1") {
|
||||||
|
if (data == null || data.isEmpty()) {
|
||||||
|
println "数据为空,无法生成Excel"
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1. 生成Excel内容
|
||||||
|
byte[] excelBytes = ExcelUtils.createExcelBytes(data, sheetName)
|
||||||
|
|
||||||
|
// 2. 上传到服务器
|
||||||
|
return XmBankVillageFtpUtils.uploadExcelBytes(excelBytes, fileName)
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
println "生成并上传Excel失败: ${e.message}"
|
||||||
|
e.printStackTrace()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def checkFileExists(def fileName){
|
||||||
|
def files = XmBankVillageFtpUtils.listOutFileNames()
|
||||||
|
def existsFlag = false
|
||||||
|
if(files){
|
||||||
|
for(def file : files){
|
||||||
|
if(file.filename == fileName){
|
||||||
|
existsFlag = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return existsFlag
|
||||||
|
}
|
||||||
|
}
|
||||||
+127
@@ -0,0 +1,127 @@
|
|||||||
|
package com.fangjia.script.gj.xiamen
|
||||||
|
|
||||||
|
import com.fangjia.script.jiangnanblank.SFTP
|
||||||
|
import com.fangjia.script.util.ExcelUtils
|
||||||
|
import com.jcraft.jsch.ChannelSftp
|
||||||
|
import org.apache.poi.ss.usermodel.Cell
|
||||||
|
import org.apache.poi.ss.usermodel.Row
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook
|
||||||
|
import org.apache.poi.ss.usermodel.WorkbookFactory
|
||||||
|
|
||||||
|
import java.nio.charset.Charset
|
||||||
|
|
||||||
|
class XmBankVillageFtpUtils {
|
||||||
|
static def ftpServer = "192.168.0.86"
|
||||||
|
static def ftpPort = 17495
|
||||||
|
static def ftpUser = "xmbank"
|
||||||
|
static def ftpPassword = "Xmbank123!@#"
|
||||||
|
static def ftpPath = "/output/village_data"
|
||||||
|
static def ftpInPath = "/input/village"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static SFTP sftp = new SFTP();
|
||||||
|
static ChannelSftp sftpChannel = sftp.connect(ftpServer, ftpPort, ftpUser, ftpPassword);
|
||||||
|
|
||||||
|
|
||||||
|
static def ftpUpload(outputPath_dist, month){
|
||||||
|
//上传到ftp
|
||||||
|
sftpChannel = reConnect();
|
||||||
|
sftp.upload(ftpPath, outputPath_dist, sftpChannel);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static def reConnect(){
|
||||||
|
if(sftpChannel == null){
|
||||||
|
println "sftpChannel 为空,需重连....."
|
||||||
|
if(sftp == null){
|
||||||
|
sftp = new SFTP();
|
||||||
|
}
|
||||||
|
sftpChannel = sftp.connect(ftpServer, ftpPort, ftpUser, ftpPassword);
|
||||||
|
}else{
|
||||||
|
println "sftpChannel 正常....."
|
||||||
|
}
|
||||||
|
return sftpChannel;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static def listOutFileNames(){
|
||||||
|
//上传到ftp
|
||||||
|
sftpChannel = reConnect();
|
||||||
|
Vector files = sftp.listFiles(ftpPath+"/", sftpChannel);
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
|
static def listInFileNames(){
|
||||||
|
//上传到ftp
|
||||||
|
sftpChannel = reConnect();
|
||||||
|
Vector files = sftp.listFiles(ftpInPath+"/", sftpChannel);
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
|
static def getFileLength(String path, String month){
|
||||||
|
sftpChannel = reConnect();
|
||||||
|
def size = sftp.getFileSize(ftpPath+ month+"/"+path, sftpChannel)
|
||||||
|
return size
|
||||||
|
}
|
||||||
|
|
||||||
|
static def readExcelFile(String fileName) {
|
||||||
|
sftpChannel = reConnect()
|
||||||
|
try {
|
||||||
|
// 构建完整的远程路径
|
||||||
|
String fullRemotePath = ftpInPath + "/"+fileName
|
||||||
|
println "开始读取Excel文件: ${fullRemotePath}"
|
||||||
|
// 从SFTP服务器获取文件流
|
||||||
|
InputStream inputStream = sftpChannel.get(fullRemotePath)
|
||||||
|
def excelData = ExcelUtils.readFromStream(inputStream, 0, 1)
|
||||||
|
println "Excel共有 ${excelData.size()} 行数据"
|
||||||
|
inputStream.close()
|
||||||
|
return excelData
|
||||||
|
} catch (Exception e) {
|
||||||
|
println "读取Excel文件失败: ${e.message}"
|
||||||
|
e.printStackTrace()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传Excel字节数组到服务器
|
||||||
|
* @param excelBytes Excel字节数组
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @return 上传是否成功
|
||||||
|
*/
|
||||||
|
private static boolean uploadExcelBytes(byte[] excelBytes, String fileName) {
|
||||||
|
sftpChannel = reConnect()
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 构建完整的远程路径
|
||||||
|
String remoteFilePath = "${ftpPath}/${fileName}"
|
||||||
|
|
||||||
|
println "开始上传Excel文件到: ${remoteFilePath}"
|
||||||
|
println "文件大小: ${excelBytes.length} 字节"
|
||||||
|
|
||||||
|
// 将字节数组转换为输入流
|
||||||
|
InputStream inputStream = new ByteArrayInputStream(excelBytes)
|
||||||
|
|
||||||
|
// 上传文件
|
||||||
|
sftpChannel.put(inputStream, remoteFilePath)
|
||||||
|
|
||||||
|
// 关闭流
|
||||||
|
inputStream.close()
|
||||||
|
|
||||||
|
println "Excel文件上传成功: ${remoteFilePath}"
|
||||||
|
|
||||||
|
return true
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
println "上传Excel文件失败: ${e.message}"
|
||||||
|
e.printStackTrace()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+8
-3
@@ -39,13 +39,13 @@ class TestEvaluation {
|
|||||||
// task.newAndOldMatch()
|
// task.newAndOldMatch()
|
||||||
|
|
||||||
// task.addFjwToBprice("D:\\数据\\宏信息\\宏信房源台账-价格.xlsx")
|
// task.addFjwToBprice("D:\\数据\\宏信息\\宏信房源台账-价格.xlsx")
|
||||||
// task.addAreaFactor()
|
task.addAreaFactor()
|
||||||
|
|
||||||
// task.addOptName()
|
// task.addOptName()
|
||||||
// task.addDistId()
|
// task.addDistId()
|
||||||
// task.addEvaluationToDB()
|
// task.addEvaluationToDB()
|
||||||
|
|
||||||
task.addEvaluationDetail()
|
// task.addEvaluationDetail()
|
||||||
}
|
}
|
||||||
|
|
||||||
def houseSimilar(){
|
def houseSimilar(){
|
||||||
@@ -298,7 +298,7 @@ class TestEvaluation {
|
|||||||
|
|
||||||
|
|
||||||
def addAreaFactor(){
|
def addAreaFactor(){
|
||||||
ExcelUtils.multiThreadingModify("D:\\数据\\宏信息\\宏信房源台账-基准价(含ID).xlsx",{d->
|
ExcelUtils.multiThreadingModify("D:\\数据\\宏信息\\(红色)宏信估值详情-最新0106.xlsx",{d->
|
||||||
if(d['I'] && d['J'] && d['K']){
|
if(d['I'] && d['J'] && d['K']){
|
||||||
def dist = CommonDatautils.findDistrictById(d['I'])
|
def dist = CommonDatautils.findDistrictById(d['I'])
|
||||||
if(dist){
|
if(dist){
|
||||||
@@ -314,6 +314,11 @@ class TestEvaluation {
|
|||||||
// }
|
// }
|
||||||
// d['L']=(int)(d['J']*d['K'])
|
// d['L']=(int)(d['J']*d['K'])
|
||||||
// d['N'] = (int)(d['L'] + d['L']*d['M'])
|
// d['N'] = (int)(d['L'] + d['L']*d['M'])
|
||||||
|
def obj = CommonDatautils.queryDistrictPriceData(dist.city, dist.region, dist.name)
|
||||||
|
if(obj && obj.avg_price_2b){
|
||||||
|
d['M'] = obj.avg_price_2b
|
||||||
|
d['L'] = (int)(obj.avg_price_2b * d['K'])
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import org.apache.poi.ss.usermodel.*
|
|||||||
import org.apache.poi.ss.util.CellRangeAddress
|
import org.apache.poi.ss.util.CellRangeAddress
|
||||||
import org.apache.poi.ss.util.RegionUtil
|
import org.apache.poi.ss.util.RegionUtil
|
||||||
import org.apache.poi.xssf.streaming.SXSSFWorkbook
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService
|
import java.util.concurrent.ExecutorService
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
@@ -103,6 +104,50 @@ public class ExcelUtils{
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static List<List<Object>> readFromStream(def inp,int sheetIndex = 0,int skipRow = 1){
|
||||||
|
List<ExcelRow<Object>> ret = new ArrayList<ExcelRow<Object>>();
|
||||||
|
try {
|
||||||
|
Workbook workbook = WorkbookFactory.create(inp)
|
||||||
|
workbook.setForceFormulaRecalculation(true)
|
||||||
|
Sheet sheet = workbook.getSheetAt(sheetIndex);
|
||||||
|
//行号
|
||||||
|
int rowIndex = 0;
|
||||||
|
for(Row row : sheet){
|
||||||
|
rowIndex ++
|
||||||
|
if(rowIndex <= skipRow){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
short minColIx = 0;
|
||||||
|
short maxColIx = row.getLastCellNum();
|
||||||
|
List<Object> rowData = new ExcelRow<Object>();
|
||||||
|
for(short colIx=minColIx; colIx<maxColIx; colIx++) {
|
||||||
|
Cell cell = row.getCell(colIx);
|
||||||
|
if(cell == null) {
|
||||||
|
rowData.add(colIx, null);
|
||||||
|
}else{
|
||||||
|
rowData.add(getCellValue(cell))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret.add(rowData);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
if (inp != null){
|
||||||
|
try {
|
||||||
|
inp.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static String getSheetName(String fileName, Integer srcSheetIndex = 0){
|
public static String getSheetName(String fileName, Integer srcSheetIndex = 0){
|
||||||
def inp = new FileInputStream(fileName);
|
def inp = new FileInputStream(fileName);
|
||||||
Workbook workbook = WorkbookFactory.create(inp)
|
Workbook workbook = WorkbookFactory.create(inp)
|
||||||
@@ -297,7 +342,7 @@ public class ExcelUtils{
|
|||||||
row = mainSheet.createRow(rowIndex);
|
row = mainSheet.createRow(rowIndex);
|
||||||
row.setHeightInPoints(30)
|
row.setHeightInPoints(30)
|
||||||
List<Object> rowDataList = data.get(rowIndex);
|
List<Object> rowDataList = data.get(rowIndex);
|
||||||
if(rowDataList == null) continue
|
if(rowDataList == null) continue
|
||||||
try{
|
try{
|
||||||
for(int cellIndex = 0 ; cellIndex < rowDataList.size();cellIndex++){
|
for(int cellIndex = 0 ; cellIndex < rowDataList.size();cellIndex++){
|
||||||
Cell cell = row.createCell(cellIndex);
|
Cell cell = row.createCell(cellIndex);
|
||||||
@@ -499,9 +544,9 @@ public class ExcelUtils{
|
|||||||
try{
|
try{
|
||||||
return cell.getStringCellValue()
|
return cell.getStringCellValue()
|
||||||
}catch (Exception ex){
|
}catch (Exception ex){
|
||||||
println cell.getColumnIndex()
|
println cell.getColumnIndex()
|
||||||
println cell.getRowIndex()
|
println cell.getRowIndex()
|
||||||
return cell.getRichStringCellValue().getString()
|
return cell.getRichStringCellValue().getString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case Cell.CELL_TYPE_NUMERIC:
|
case Cell.CELL_TYPE_NUMERIC:
|
||||||
@@ -515,4 +560,220 @@ public class ExcelUtils{
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建Excel字节数组
|
||||||
|
* @param data Excel数据
|
||||||
|
* @param sheetName Sheet名称
|
||||||
|
* @return Excel文件的字节数组
|
||||||
|
*/
|
||||||
|
private static byte[] createExcelBytes(List<List<String>> data, String sheetName) {
|
||||||
|
Workbook workbook = null
|
||||||
|
ByteArrayOutputStream outputStream = null
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 创建新的工作簿(使用.xlsx格式)
|
||||||
|
workbook = new XSSFWorkbook()
|
||||||
|
|
||||||
|
// 创建样式(可选)
|
||||||
|
CellStyle headerStyle = createHeaderStyle(workbook)
|
||||||
|
CellStyle dataStyle = createDataStyle(workbook)
|
||||||
|
|
||||||
|
// 创建Sheet
|
||||||
|
Sheet sheet = workbook.createSheet(sheetName)
|
||||||
|
sheet.setColumnWidth(0, 5 * 256)
|
||||||
|
sheet.setColumnWidth(1, 100 * 256)
|
||||||
|
sheet.setColumnWidth(2, 20 * 256)
|
||||||
|
sheet.setColumnWidth(3, 50 * 256)
|
||||||
|
|
||||||
|
// 写入数据
|
||||||
|
writeDataToSheet(sheet, data, headerStyle, dataStyle)
|
||||||
|
|
||||||
|
// 自动调整列宽(可选)
|
||||||
|
// autoSizeColumns(sheet, data)
|
||||||
|
|
||||||
|
// 将工作簿写入字节数组
|
||||||
|
outputStream = new ByteArrayOutputStream()
|
||||||
|
workbook.write(outputStream)
|
||||||
|
|
||||||
|
return outputStream.toByteArray()
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("创建Excel失败: ${e.message}", e)
|
||||||
|
} finally {
|
||||||
|
// 关闭资源
|
||||||
|
try {
|
||||||
|
if (outputStream != null) {
|
||||||
|
outputStream.close()
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建表头样式
|
||||||
|
*/
|
||||||
|
private static CellStyle createHeaderStyle(Workbook workbook) {
|
||||||
|
CellStyle style = workbook.createCellStyle()
|
||||||
|
|
||||||
|
// 设置字体
|
||||||
|
Font font = workbook.createFont()
|
||||||
|
// font.setBoldweight(7)
|
||||||
|
font.setBoldweight(Font.BOLDWEIGHT_BOLD)
|
||||||
|
font.setFontHeightInPoints((short) 12)
|
||||||
|
font.setColor(IndexedColors.BLACK.getIndex())
|
||||||
|
style.setFont(font)
|
||||||
|
|
||||||
|
// 设置背景色
|
||||||
|
style.setFillForegroundColor(IndexedColors.TURQUOISE.getIndex())
|
||||||
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND)
|
||||||
|
|
||||||
|
// 设置边框
|
||||||
|
// style.setBorderBottom(BorderStyle.THIN)
|
||||||
|
// style.setBorderTop(BorderStyle.THIN)
|
||||||
|
// style.setBorderRight(BorderStyle.THIN)
|
||||||
|
// style.setBorderLeft(BorderStyle.THIN)
|
||||||
|
|
||||||
|
// 居中
|
||||||
|
style.setAlignment(HorizontalAlignment.CENTER)
|
||||||
|
style.setVerticalAlignment(VerticalAlignment.CENTER)
|
||||||
|
|
||||||
|
return style
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建数据样式
|
||||||
|
*/
|
||||||
|
private static CellStyle createDataStyle(Workbook workbook) {
|
||||||
|
CellStyle style = workbook.createCellStyle()
|
||||||
|
|
||||||
|
// 设置边框
|
||||||
|
// style.setBorderBottom(BorderStyle.THIN)
|
||||||
|
// style.setBorderTop(BorderStyle.THIN)
|
||||||
|
// style.setBorderRight(BorderStyle.THIN)
|
||||||
|
// style.setBorderLeft(BorderStyle.THIN)
|
||||||
|
|
||||||
|
// 自动换行
|
||||||
|
style.setWrapText(true)
|
||||||
|
|
||||||
|
return style
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自动调整列宽
|
||||||
|
*/
|
||||||
|
private static void autoSizeColumns(Sheet sheet, List<List<String>> data) {
|
||||||
|
if (data.isEmpty()) return
|
||||||
|
|
||||||
|
int maxColumns = data.max { it.size() }?.size() ?: 0
|
||||||
|
|
||||||
|
for (int i = 0; i < maxColumns; i++) {
|
||||||
|
sheet.autoSizeColumn(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 写入数据到Sheet
|
||||||
|
*/
|
||||||
|
private static void writeDataToSheet(Sheet sheet, List<List<String>> data,
|
||||||
|
CellStyle headerStyle, CellStyle dataStyle) {
|
||||||
|
// 判断是否有表头(第一行是否为表头)
|
||||||
|
boolean hasHeader = data.size() > 0
|
||||||
|
|
||||||
|
for (int rowIndex = 0; rowIndex < data.size(); rowIndex++) {
|
||||||
|
List<String> rowData = data[rowIndex]
|
||||||
|
Row row = sheet.createRow(rowIndex)
|
||||||
|
|
||||||
|
if(rowIndex == 0){
|
||||||
|
row.setHeightInPoints(35.0)
|
||||||
|
}
|
||||||
|
for (int colIndex = 0; colIndex < rowData.size(); colIndex++) {
|
||||||
|
Cell cell = row.createCell(colIndex)
|
||||||
|
String cellValue = rowData[colIndex] ?: ""
|
||||||
|
|
||||||
|
// 设置单元格值
|
||||||
|
setCellValue(cell, cellValue)
|
||||||
|
|
||||||
|
// 应用样式
|
||||||
|
if (rowIndex == 0 && hasHeader && headerStyle != null) {
|
||||||
|
cell.setCellStyle(headerStyle)
|
||||||
|
} else if (dataStyle != null) {
|
||||||
|
cell.setCellStyle(dataStyle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置单元格值(智能识别类型)
|
||||||
|
*/
|
||||||
|
private static void setCellValue(Cell cell, String value) {
|
||||||
|
if (value == null || value.trim().isEmpty()) {
|
||||||
|
cell.setCellValue("")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试判断是否为数字
|
||||||
|
if (isNumeric(value)) {
|
||||||
|
try {
|
||||||
|
if (value.contains(".")) {
|
||||||
|
cell.setCellValue(Double.parseDouble(value))
|
||||||
|
} else {
|
||||||
|
cell.setCellValue(Long.parseLong(value))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 如果不是数字,继续
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试判断是否为日期
|
||||||
|
if (isDate(value)) {
|
||||||
|
try {
|
||||||
|
// 这里可以添加日期格式转换逻辑
|
||||||
|
// 暂时按字符串处理
|
||||||
|
cell.setCellValue(value)
|
||||||
|
} catch (Exception e) {
|
||||||
|
cell.setCellValue(value)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 默认为字符串
|
||||||
|
cell.setCellValue(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查字符串是否为数字
|
||||||
|
*/
|
||||||
|
private static boolean isNumeric(String str) {
|
||||||
|
if (str == null || str.isEmpty()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// 允许小数点、负号
|
||||||
|
return str.matches("-?\\d+(\\.\\d+)?")
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查字符串是否为日期(简单判断)
|
||||||
|
*/
|
||||||
|
private static boolean isDate(String str) {
|
||||||
|
if (str == null || str.isEmpty()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// 常见的日期格式模式
|
||||||
|
def datePatterns = [
|
||||||
|
/^\d{4}-\d{2}-\d{2}$/,
|
||||||
|
/^\d{4}\/\d{2}\/\d{2}$/,
|
||||||
|
/^\d{4}年\d{2}月\d{2}日$/,
|
||||||
|
/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/
|
||||||
|
]
|
||||||
|
|
||||||
|
return datePatterns.any { pattern -> str =~ pattern }
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user