|
|
|
@ -1,5 +1,8 @@
|
|
|
|
|
package UnitTest.ImportExcel;
|
|
|
|
|
|
|
|
|
|
import com.aspose.cells.Cells;
|
|
|
|
|
import com.aspose.cells.License;
|
|
|
|
|
import com.aspose.cells.Worksheet;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
@ -7,6 +10,9 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
|
|
import static org.example.AsposeUtil.getLicense;
|
|
|
|
|
|
|
|
|
|
public class TestTitle {
|
|
|
|
|
|
|
|
|
@ -21,8 +27,8 @@ public class TestTitle {
|
|
|
|
|
* @param colorIdx
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Workbook addComment(Workbook wb, int sheetIndex, int r, int c,
|
|
|
|
|
int fontSize, short colorIdx, String str) {
|
|
|
|
|
public static void addComment(Workbook wb, int sheetIndex, int r, int c,
|
|
|
|
|
int fontSize, short colorIdx, String str) {
|
|
|
|
|
Sheet sheet = wb.getSheetAt(sheetIndex);
|
|
|
|
|
Row row = sheet.getRow(r);
|
|
|
|
|
Cell cell = row.getCell(c);
|
|
|
|
@ -63,7 +69,6 @@ public class TestTitle {
|
|
|
|
|
comment.setString(factory.createRichTextString(str));
|
|
|
|
|
// 将批注添加到单元格
|
|
|
|
|
cell.setCellComment(comment);
|
|
|
|
|
return wb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -77,7 +82,7 @@ public class TestTitle {
|
|
|
|
|
* @param lastCol
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Workbook addValidation(Workbook workbook, int sheetIdx, String[] options, int firstRow, int lastRow, int firstCol, int lastCol) {
|
|
|
|
|
public static void addValidation(Workbook workbook, int sheetIdx, String[] options, int firstRow, int lastRow, int firstCol, int lastCol) {
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(sheetIdx);
|
|
|
|
|
// 创建数据验证对象
|
|
|
|
|
DataValidationHelper validationHelper = sheet.getDataValidationHelper();
|
|
|
|
@ -86,29 +91,74 @@ public class TestTitle {
|
|
|
|
|
DataValidation validation = validationHelper.createValidation(constraint, addressList);
|
|
|
|
|
// 应用数据验证到单元格
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
return workbook;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\";
|
|
|
|
|
//工作目录
|
|
|
|
|
public static String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:将多个EXCEL全部导入到数据库后,再保留表头,清空所有数据,然后增加第一列,列名:单位,再从数据库中将数据读取出来,依次填入
|
|
|
|
|
*
|
|
|
|
|
* @param filePath
|
|
|
|
|
*/
|
|
|
|
|
public static void getSummary(String filePath,String filePath2) throws Exception {
|
|
|
|
|
// 测试破解后的 aspose-cells-20.7-crack.jar
|
|
|
|
|
boolean auth = authrolizeLicense();
|
|
|
|
|
if (!auth) {
|
|
|
|
|
System.out.println("aspose 许可无效!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
System.out.println("aspose 已就绪!");
|
|
|
|
|
|
|
|
|
|
com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(filePath);
|
|
|
|
|
Worksheet sheet = wb.getWorksheets().get(0);
|
|
|
|
|
Cells cells = sheet.getCells();
|
|
|
|
|
cells.insertColumns(0, 1);
|
|
|
|
|
wb.save(filePath2);
|
|
|
|
|
}
|
|
|
|
|
public static boolean authrolizeLicense() {
|
|
|
|
|
boolean result = false;
|
|
|
|
|
try {
|
|
|
|
|
InputStream is = com.aspose.cells.License.class.getResourceAsStream("/com.aspose.cells.lic_2999.xml");
|
|
|
|
|
License asposeLicense = new License();
|
|
|
|
|
asposeLicense.setLicense(is);
|
|
|
|
|
is.close();
|
|
|
|
|
result = true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
//学校上传的Excel
|
|
|
|
|
String filePath = path + "1.xlsx";
|
|
|
|
|
String filePath2 = path + "2.xlsx";
|
|
|
|
|
//要进行标注的行和列
|
|
|
|
|
int r = 5, c = 6;//第6行第7列
|
|
|
|
|
//字号
|
|
|
|
|
int fontSize = 12;
|
|
|
|
|
//标识的颜色,比如黄色、白色
|
|
|
|
|
short colorIdx = IndexedColors.YELLOW.getIndex();
|
|
|
|
|
|
|
|
|
|
// 创建下拉框的选项
|
|
|
|
|
String[] options = new String[]{"男", "女", "未知"};
|
|
|
|
|
|
|
|
|
|
//对文件进行校验,标注
|
|
|
|
|
try (FileInputStream fileIn = new FileInputStream(filePath);
|
|
|
|
|
Workbook workbook = new XSSFWorkbook(fileIn)) {
|
|
|
|
|
//在指定单元格上添加背景黄色+标注提示信息
|
|
|
|
|
addComment(workbook, 0, r, c, fontSize, colorIdx, "此单元格应该是非空的!");
|
|
|
|
|
//添加下拉框校验
|
|
|
|
|
addValidation(workbook, 0, options, 4, 21, c - 1, c - 1);
|
|
|
|
|
// 保存到文件
|
|
|
|
|
addValidation(workbook, 0, options, 4, 21, c - 1, c - 1);//范围
|
|
|
|
|
// 保存
|
|
|
|
|
FileOutputStream fileOut = new FileOutputStream(filePath);
|
|
|
|
|
workbook.write(fileOut);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//生成汇总文件,并增加一列,一般是单位名称
|
|
|
|
|
getSummary(filePath,filePath2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|