Initial commit
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package com.mingliqiye.lida.waidiao
|
||||
|
||||
import com.mingliqiye.logger.*
|
||||
import com.mingliqiye.utils.time.DateTime
|
||||
import org.apache.poi.ss.usermodel.WorkbookFactory
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
|
||||
private val log = getLogger("Main")
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
// 参数校验
|
||||
if (args.isEmpty()) {
|
||||
log error "用法: <输入文件路径> [输出目录]"
|
||||
log error "示例: java -jar app.jar D:/data/MultiSheet.xlsx D:/output/"
|
||||
return
|
||||
}
|
||||
|
||||
val sourceFilePath = args[0]
|
||||
val sourceFile = File(sourceFilePath)
|
||||
|
||||
if (!sourceFile.exists() || !sourceFile.isFile) {
|
||||
log error "错误: 文件不存在 -> $sourceFilePath"
|
||||
return
|
||||
}
|
||||
|
||||
val outputDir = if (args.size >= 2) {
|
||||
File(args[1])
|
||||
} else {
|
||||
File(sourceFile.parentFile, sourceFile.nameWithoutExtension)
|
||||
}
|
||||
|
||||
if (!outputDir.exists()) {
|
||||
outputDir.mkdirs()
|
||||
}
|
||||
|
||||
// 输出文件扩展名与源文件保持一致
|
||||
val extension = sourceFile.extension.ifEmpty { "xlsx" }
|
||||
|
||||
log info "源文件: ${sourceFile.absolutePath}"
|
||||
log info "输出目录: ${outputDir.absolutePath}"
|
||||
|
||||
try {
|
||||
val sheetNames = FileInputStream(sourceFile).use { fis ->
|
||||
WorkbookFactory.create(fis).use { wb ->
|
||||
(0 until wb.numberOfSheets).map { wb.getSheetName(it) }
|
||||
}
|
||||
}
|
||||
|
||||
log info "共发现 ${sheetNames.size} 个 Sheet,开始拆分..."
|
||||
|
||||
sheetNames.forEachIndexed { index, sheetName ->
|
||||
if (sheetName == "总表"){
|
||||
return@forEachIndexed
|
||||
}
|
||||
// 每次重新读取原始文件,保证副本纯净
|
||||
FileInputStream(sourceFile).use { fis ->
|
||||
WorkbookFactory.create(fis).use { workbook ->
|
||||
// 从后往前删除,避免索引错乱
|
||||
for (i in workbook.numberOfSheets - 1 downTo 0) {
|
||||
if (i != index) {
|
||||
workbook.removeSheetAt(i)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val safeName = "利达监管库出库确认单-${sheetName.replace(" ", "")}${DateTime.now().format("yyyy.MM.dd")}"
|
||||
val outputFile = File(outputDir, "$safeName.$extension")
|
||||
|
||||
FileOutputStream(outputFile).use { fos ->
|
||||
workbook.write(fos)
|
||||
}
|
||||
|
||||
log info "[$index] 已生成: ${outputFile.absolutePath}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log info "拆分完成,共生成 ${sheetNames.size} 个文件。"
|
||||
} catch (e: Exception) {
|
||||
log.error("处理失败: ${e.message}",e)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user