main
黄海 2 years ago
parent 82a38e2755
commit 57e1080d01

@ -1,12 +1,11 @@
package UnitTest.ImportExcel;
import com.dsideal.QingLong.Util.AsposeUtil;
import com.dsideal.QingLong.Util.ExcelUtil;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import org.apache.poi.openxml4j.util.ZipSecureFile;
@ -31,8 +30,6 @@ public class ImportExcelData {
public static String person_id = "c8ef14f2-5dcc-4b62-8964-2282b68ba21d";
public static void main(String[] args) throws IOException, ParseException {
//加载License
AsposeUtil.getLicense();
// 延迟解析比率
ZipSecureFile.setMinInflateRatio(-1.0d);
//告之配置文件位置
@ -51,6 +48,7 @@ public class ImportExcelData {
String f1 = path + File.separator + upload_excel_filename;
//用户上传的填充完的EXCEL文件
String f2 = path + File.separator + "school.xlsx";
//对比两个EXCEL文件 是不是格式一致,也就是是不是上传了正确的模板文件
int sheetCnt = ExcelUtil.getSheetCount(upload_excel_filename);
for (int i = 0; i < sheetCnt; i++) {
@ -72,7 +70,6 @@ public class ImportExcelData {
XSSFSheet sheet = wb.getSheetAt(i);
//移除所有批注
ExcelUtil.RemoveAllComment(sheet);
//数据起始行
Record r = ExcelUtil.getSheetConfig(upload_excel_filename, i);
int data_start_row = r.getInt("data_start_row");

@ -1,31 +0,0 @@
package UnitTest.ImportExcel;
import com.dsideal.QingLong.Util.ExcelUtil;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestDiffrentColor {
public static void main(String[] args) throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("RichTextStringExample");
XSSFRow row = sheet.createRow(0);//行号
XSSFCell cell = row.createCell(0);//单元格
cell.setCellValue("我是原来的内容");
//添加重点提示符*和Comment
ExcelUtil.addStar(wb, cell);
//添加Comment
ExcelUtil.addComment(wb, cell, "此单元格内容必须输入!");
//输出
FileOutputStream fileOut = new FileOutputStream("c:\\workbook.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
}

@ -1,61 +0,0 @@
package UnitTest.ImportExcel;
import com.dsideal.QingLong.Util.ExcelUtil;
import com.jfinal.kit.StrKit;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
public class TestReadMergeCell {
/**
*
*
* @param sheet
* @param row
* @param col
* @return
*/
public static boolean isMerged(XSSFSheet sheet, int row, int col) {
// 判断单元格是否被合并
for (CellRangeAddress range : sheet.getMergedRegions()) {
if (row >= range.getFirstRow() && row <= range.getLastRow()
&& col >= range.getFirstColumn() && col <= range.getLastColumn()) {
return true;
}
}
return false;
}
public static void main(String[] args) throws IOException {
String source = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\source.xlsx";
int row = 3; // 假设要判断的单元格的行号
int col = 1; // 假设要判断的单元格的列号
InputStream is = new FileInputStream(source);
XSSFWorkbook wb = new XSSFWorkbook(is);
XSSFSheet sheet = wb.getSheetAt(0);
for (int i = row; i <= sheet.getLastRowNum(); i++) {
if (isMerged(sheet, i, col)) {
XSSFCell cell = sheet.getRow(i).getCell(col);
String value = ExcelUtil.getValue(cell).toString();
if (StrKit.isBlank(value)) {
for (int j = i - 1; ; j--) {
String prev = ExcelUtil.getValue(sheet.getRow(j).getCell(col)).toString();
if (!StrKit.isBlank(prev)) {
System.out.println(prev);
break;
}
}
} else {
System.out.println(ExcelUtil.getValue(cell));
}
}
}
}
}

@ -19,18 +19,6 @@ import java.util.*;
import static org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted;
public class ExcelUtil {
/**
*
*
* @param str
* @return
*/
public static String removeKuoHao(String str) {
str = str.replace("\n", "");
String output = str.replaceAll("\\(.*?\\)", "");
output = output.replaceAll("\\.*?\\", "");
return output;
}
/**
* )
@ -88,53 +76,6 @@ public class ExcelUtil {
return list;
}
/**
*
*
* @param tableName
* @return
*/
public static boolean isTableExist(String tableName) {
String sql = "select count(*) as c from pg_class where relname = ?";
return Db.findFirst(sql, tableName).getInt("c") == 1;
}
/**
* +Sheet
*
* @param upload_excel_filename
* @param sheet_index
* @return
*/
public static Record getSheetConfig(String upload_excel_filename, int sheet_index) {
String sql = "select * from t_importexcel_config where upload_excel_filename=? and sheet_index=?";
return Db.findFirst(sql, upload_excel_filename, sheet_index);
}
/**
* +Sheet
*
* @param upload_excel_filename
* @return
*/
public static int getSheetCount(String upload_excel_filename) {
String sql = "select * from t_importexcel_config where upload_excel_filename=?";
List<Record> list = Db.find(sql, upload_excel_filename);
return list.size();
}
/**
* +Sheet
*
* @param upload_excel_filename
* @param sheet_index
* @return
*/
public static List<Record> getSheetMapping(String upload_excel_filename, int sheet_index) {
String sql = "select * from t_importexcel_mapping where upload_excel_filename=? and sheet_index=?";
return Db.find(sql, upload_excel_filename, sheet_index);
}
/**
* EXCEL+ Map.Entry<Integer, Integer>
*
@ -352,107 +293,51 @@ public class ExcelUtil {
String sql = "drop table if exists " + tableName;
Db.update(sql);
}
/**
*
*
*
* @param sheet
* @param tableName
* @return
*/
public static boolean isMerged(XSSFSheet sheet, XSSFCell cell) {
// 判断单元格是否被合并
for (CellRangeAddress range : sheet.getMergedRegions()) {
if (cell.getRowIndex() >= range.getFirstRow() && cell.getRowIndex() <= range.getLastRow()
&& cell.getColumnIndex() >= range.getFirstColumn() && cell.getColumnIndex() <= range.getLastColumn()) {
return true;
}
}
return false;
public static boolean isTableExist(String tableName) {
String sql = "select count(*) as c from pg_class where relname = ?";
return Db.findFirst(sql, tableName).getInt("c") == 1;
}
/**
*
* +Sheet
*
* @throws ParseException
* @param upload_excel_filename
* @param sheet_index
* @return
*/
public static void importData(String upload_excel_filename, XSSFWorkbook wb,
int sheetIdx, String bureau_id, String person_id) throws ParseException {
//读取sheet页
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
//通过表名获取到它的读取起始行,终止列
public static Record getSheetConfig(String upload_excel_filename, int sheet_index) {
String sql = "select * from t_importexcel_config where upload_excel_filename=? and sheet_index=?";
Record record = Db.findFirst(sql, upload_excel_filename, sheetIdx);
String table_name = record.getStr("table_name");
//先删除
sql = "delete from " + table_name + " where bureau_id=?";
Db.update(sql, bureau_id);
int data_start_row = record.getInt("data_start_row");
int column_num = record.getInt("column_num");
//获取字段与EXCEL列的映射信息
sql = "select * from t_importexcel_mapping where upload_excel_filename=? and sheet_index=?";
List<Record> list = Db.find(sql, upload_excel_filename, sheetIdx);
Map<Integer, Record> _map = new HashMap<>();
for (Record r : list) {
int excel_column_idx = r.getInt("excel_column_idx");
String column_name = r.getStr("column_name");
String column_type = r.getStr("column_type");
Record rt = new Record();
rt.set("column_name", column_name);
rt.set("column_type", column_type);
_map.put(excel_column_idx, rt);
}
//开始读取数据
List<Record> writeList = new ArrayList<>();
for (int i = data_start_row; i <= sheet.getLastRowNum(); i++) {
//获得行
XSSFRow row = sheet.getRow(i);
return Db.findFirst(sql, upload_excel_filename, sheet_index);
}
//遍历列
if (row != null) {
Record writeRecord = new Record();
writeRecord.set("bureau_id", bureau_id);
writeRecord.set("person_id", person_id);
for (int j = 0; j < column_num; j++) {
XSSFCell cell = row.getCell(j);
String colType = _map.get(j).getStr("column_type");
String colName = _map.get(j).getStr("column_name");
/**
* +Sheet
*
* @param upload_excel_filename
* @return
*/
public static int getSheetCount(String upload_excel_filename) {
String sql = "select * from t_importexcel_config where upload_excel_filename=?";
List<Record> list = Db.find(sql, upload_excel_filename);
return list.size();
}
//cell可能是被合并的单元格
if (isMerged(sheet, cell)) {
String value = ExcelUtil.getValue(cell).toString();
if (StrKit.isBlank(value)) {
for (int k = i - 1; ; k--) {
String prev = ExcelUtil.getValue(sheet.getRow(k).getCell(j)).toString();
if (!StrKit.isBlank(prev)) {
value = prev;
break;
}
}
}
writeRecord.set(colName, value);
} else {
if (colType.equals("Integer"))
writeRecord.set(colName, (int) Double.parseDouble(getValue(cell).toString()));
else if (colType.equals("String"))
writeRecord.set(colName, getValue(cell).toString());
else if (colType.equals("Double"))
writeRecord.set(colName, Double.parseDouble(getValue(cell).toString()));
else if (colType.equals("Date")) {
String dateString = getValue(cell).toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(dateString);
writeRecord.set(colName, date);
}
}
}
writeList.add(writeRecord);
}
}
// 写入数据
Db.batchSave(table_name, writeList, 100);
/**
* +Sheet
*
* @param upload_excel_filename
* @param sheet_index
* @return
*/
public static List<Record> getSheetMapping(String upload_excel_filename, int sheet_index) {
String sql = "select * from t_importexcel_mapping where upload_excel_filename=? and sheet_index=?";
return Db.find(sql, upload_excel_filename, sheet_index);
}
@ -692,4 +577,119 @@ public class ExcelUtil {
return false;
}
}
/**
*
*
* @param sheet
* @return
*/
public static boolean isMerged(XSSFSheet sheet, XSSFCell cell) {
// 判断单元格是否被合并
for (CellRangeAddress range : sheet.getMergedRegions()) {
if (cell.getRowIndex() >= range.getFirstRow() && cell.getRowIndex() <= range.getLastRow()
&& cell.getColumnIndex() >= range.getFirstColumn() && cell.getColumnIndex() <= range.getLastColumn()) {
return true;
}
}
return false;
}
/**
*
*
* @param str
* @return
*/
public static String removeKuoHao(String str) {
str = str.replace("\n", "");
String output = str.replaceAll("\\(.*?\\)", "");
output = output.replaceAll("\\.*?\\", "");
return output;
}
/**
*
*
* @throws ParseException
*/
public static void importData(String upload_excel_filename, XSSFWorkbook wb,
int sheetIdx, String bureau_id, String person_id) throws ParseException {
//读取sheet页
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
//通过表名获取到它的读取起始行,终止列
String sql = "select * from t_importexcel_config where upload_excel_filename=? and sheet_index=?";
Record record = Db.findFirst(sql, upload_excel_filename, sheetIdx);
String table_name = record.getStr("table_name");
//先删除
sql = "delete from " + table_name + " where bureau_id=?";
Db.update(sql, bureau_id);
int data_start_row = record.getInt("data_start_row");
int column_num = record.getInt("column_num");
//获取字段与EXCEL列的映射信息
sql = "select * from t_importexcel_mapping where upload_excel_filename=? and sheet_index=?";
List<Record> list = Db.find(sql, upload_excel_filename, sheetIdx);
Map<Integer, Record> _map = new HashMap<>();
for (Record r : list) {
int excel_column_idx = r.getInt("excel_column_idx");
String column_name = r.getStr("column_name");
String column_type = r.getStr("column_type");
Record rt = new Record();
rt.set("column_name", column_name);
rt.set("column_type", column_type);
_map.put(excel_column_idx, rt);
}
//开始读取数据
List<Record> writeList = new ArrayList<>();
for (int i = data_start_row; i <= sheet.getLastRowNum(); i++) {
//获得行
XSSFRow row = sheet.getRow(i);
//遍历列
if (row != null) {
Record writeRecord = new Record();
writeRecord.set("bureau_id", bureau_id);
writeRecord.set("person_id", person_id);
for (int j = 0; j < column_num; j++) {
XSSFCell cell = row.getCell(j);
String colType = _map.get(j).getStr("column_type");
String colName = _map.get(j).getStr("column_name");
//cell可能是被合并的单元格
if (isMerged(sheet, cell)) {
String value = ExcelUtil.getValue(cell).toString();
if (StrKit.isBlank(value)) {
for (int k = i - 1; ; k--) {
String prev = ExcelUtil.getValue(sheet.getRow(k).getCell(j)).toString();
if (!StrKit.isBlank(prev)) {
value = prev;
break;
}
}
}
writeRecord.set(colName, value);
} else {
if (colType.equals("Integer"))
writeRecord.set(colName, (int) Double.parseDouble(getValue(cell).toString()));
else if (colType.equals("String"))
writeRecord.set(colName, getValue(cell).toString());
else if (colType.equals("Double"))
writeRecord.set(colName, Double.parseDouble(getValue(cell).toString()));
else if (colType.equals("Date")) {
String dateString = getValue(cell).toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(dateString);
writeRecord.set(colName, date);
}
}
}
writeList.add(writeRecord);
}
}
// 写入数据
Db.batchSave(table_name, writeList, 100);
}
}

Loading…
Cancel
Save