main
黄海 8 months ago
parent 83fda850fc
commit 6f0279745f

@ -28,7 +28,7 @@ public class C7_Test {
static String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【7】概览\\概览.xlsx";
//源文件
static String sourceExcel = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【7】概览\\(昭通市)汇总表.xls";
static String sourceExcel = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【7】概览\\(昭通市)汇总表.xlsx";
public static void main(String[] args) throws IOException, InvalidFormatException {
//初始化数据库连接
@ -36,6 +36,10 @@ public class C7_Test {
//实例化
ReadDocxUtil ru = new ReadDocxUtil();
//如果源文件或者示例文件是xls格式则转化为xlsx格式
sourceExcel = ExcelKit.ConvertXlsToXlsx(sourceExcel);
sampleExcelPath = ExcelKit.ConvertXlsToXlsx(sampleExcelPath);
//目标Excel,就是把文件名解析出来后,后面添加上【成果】,需要动态计算获取,不能写死
String excelPath = sampleExcelPath.replace(".xlsx", "【成果】.xlsx");
ExcelKit.delExcel(excelPath);
@ -52,48 +56,54 @@ public class C7_Test {
ExcelKit.CopyHead(sampleExcelPath, outSheet, headerStyle);
//1、用POI打开指定文件EXCEL获取它的最后一个有效数据行就是这个城市的整体数据汇总
List<List<String>> sheetList = ExcelKit.readSheet(sourceExcel);
System.out.println(sheetList);
/*2
I
O
P
M
N
S
Z
AA
X
Y
AD
AK
AH
AI
AN
AQ
AR
AO
AP
AU
AX
AY
AV
AW
*/
List<List<String>> sheetList = ExcelKit.readSheet(sourceExcel,4);
List<String> stageList = new ArrayList<>();
stageList.add("学前");
stageList.add("小学");
stageList.add("初中");
stageList.add("高中");
stageList.add("中职");
for (List<String> strings : sheetList) {
for (String string : strings) {
System.out.print(string+" ");
}
System.out.println();
}
//
// /*2、按下面的列号读取相应的数据
// 学前:
// 学校总数: I
// 招生总数: O
// 在校生总数P
// 教职工总数M
// 专任教师数N
// 小学:
// 学校总数: S
// 招生总数: Z
// 在校生总数AA
// 教职工总数X
// 专任教师数Y
// 初中:
// 学校总数:
// 招生总数: AD
// 在校生总数AK
// 教职工总数AH
// 专任教师数AI
// 高中:
// 学校总数: AN
// 招生总数: AQ
// 在校生总数AR
// 教职工总数AO
// 专任教师数AP
// 中职:
// 学校总数: AU
// 招生总数: AX
// 在校生总数AY
// 教职工总数AV
// 专任教师数AW
// */
//
// List<String> stageList = new ArrayList<>();
// stageList.add("学前");
// stageList.add("小学");
// stageList.add("初中");
// stageList.add("高中");
// stageList.add("中职");
// for(String stage:stageList){
// //按规则获取当前学段中的学校总数,招生总数,在校生总数,教职工总数,专任教师数

@ -14,6 +14,24 @@ import java.util.ArrayList;
import java.util.List;
public class ExcelKit {
/**
* xlsxlsx
*
* @param sourceExcel
* @throws IOException
*/
public static String ConvertXlsToXlsx(String sourceExcel) throws IOException {
if (sourceExcel.endsWith(".xls")) {
ExcelUtil.xlsChangeXlsx(sourceExcel, sourceExcel + "x");
//删除旧的文件
ExcelKit.delExcel(sourceExcel);
sourceExcel = sourceExcel + "x";
System.out.println("源文件" + sourceExcel + "文件转换成功");
}
return sourceExcel;
}
/**
*
*
@ -108,18 +126,15 @@ public class ExcelKit {
/**
* @throws IOException
*/
public static List<List<String>> readSheet(String sourceExcel) throws IOException {
//如果文件是xls格式则转换为xlsx格式
if (sourceExcel.endsWith(".xls")) {
ExcelUtil.xlsChangeXlsx(sourceExcel, sourceExcel + "x");
sourceExcel = sourceExcel + "x";
//删除旧的文件
delExcel(sourceExcel);
}
return readSheet(new XSSFWorkbook(new FileInputStream(sourceExcel)));
public static List<List<String>> readSheet(String sourceExcel, int skipRowCount) throws IOException {
return readSheet(new XSSFWorkbook(new FileInputStream(sourceExcel)), skipRowCount);
}
public static List<List<String>> readSheet(XSSFWorkbook workbook) throws IOException {
return readSheet(workbook, 1);
}
public static List<List<String>> readSheet(XSSFWorkbook workbook, int skipRowCount) throws IOException {
XSSFSheet sheet = workbook.getSheet("Sheet1");
List<List<String>> array = new ArrayList<>();
//遍历输出sheet的内容
@ -128,7 +143,7 @@ public class ExcelKit {
if (sheet == null) return array;
for (Row row : sheet) {
rowIndex++;
if (rowIndex == 1) continue;//第一行为表头
if (rowIndex <= skipRowCount) continue;//跳过指定的行数
if (row == null) break;
if (row.getCell(0) == null) break;

@ -1,14 +0,0 @@
package com.dsideal.base.Tools.FillData.Test;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelUtil;
import java.io.IOException;
public class ConvertXls {
public static void main(String[] args) throws IOException {
String inputFile = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【7】概览\\(昭通市)汇总表.xls";
String outputFile = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【7】概览\\(昭通市)汇总表.xlsx";
ExcelUtil.xlsChangeXlsx(inputFile, outputFile);
}
}
Loading…
Cancel
Save