|
|
@ -26,6 +26,7 @@ import java.util.Map; |
|
|
|
|
|
|
|
/** |
|
|
|
* 导入导出excel |
|
|
|
* @author wu |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
@Component |
|
|
@ -33,8 +34,17 @@ public class PoiUtil { |
|
|
|
|
|
|
|
@Data |
|
|
|
public static class PoiUtilCell { |
|
|
|
/** |
|
|
|
* 单元格内容 |
|
|
|
*/ |
|
|
|
private String value = ""; |
|
|
|
/** |
|
|
|
* 跨列 |
|
|
|
*/ |
|
|
|
private int colspan = 1; |
|
|
|
/** |
|
|
|
* 跨行 |
|
|
|
*/ |
|
|
|
private int rowspan = 1; |
|
|
|
/** |
|
|
|
* 水平居中 |
|
|
@ -63,6 +73,7 @@ public class PoiUtil { |
|
|
|
private String function; |
|
|
|
|
|
|
|
public PoiUtilCell() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public PoiUtilCell(String value) { |
|
|
@ -104,7 +115,7 @@ public class PoiUtil { |
|
|
|
* @param sheetName sheet名称 |
|
|
|
* @param rows 行 |
|
|
|
* @param wb XSSFWorkbook对象 无则创建 |
|
|
|
* @return |
|
|
|
* @return 返回生成的excel数据 |
|
|
|
*/ |
|
|
|
public static Workbook exportWB(String sheetName, List<List<PoiUtilCell>> rows, Workbook wb) { |
|
|
|
|
|
|
@ -186,11 +197,10 @@ public class PoiUtil { |
|
|
|
/** |
|
|
|
* 合并单元格 |
|
|
|
* |
|
|
|
* @param sheet |
|
|
|
* @param rows |
|
|
|
* @param cols |
|
|
|
* @param cell |
|
|
|
* @return |
|
|
|
* @param sheet 当前sheet |
|
|
|
* @param rows 行数 |
|
|
|
* @param cols 列数 |
|
|
|
* @param cell 单元格信息 |
|
|
|
*/ |
|
|
|
private static void mergedRegion(Sheet sheet, int rows, int cols, PoiUtilCell cell) { |
|
|
|
//
|
|
|
@ -430,7 +440,6 @@ public class PoiUtil { |
|
|
|
|
|
|
|
/** |
|
|
|
* 生成sheet |
|
|
|
* |
|
|
|
* @param file |
|
|
|
* @param index |
|
|
|
* @return |
|
|
@ -491,79 +500,15 @@ public class PoiUtil { |
|
|
|
anchor.setCol2(col2); |
|
|
|
// 插入图片
|
|
|
|
Picture pict = drawing.createPicture(anchor, pictureIdx); |
|
|
|
|
|
|
|
// FileOutputStream fileOut = null;
|
|
|
|
// fileOut = new FileOutputStream("D:/测试Excel.xlsx");
|
|
|
|
// // 写入excel文件
|
|
|
|
// wb.write(fileOut);
|
|
|
|
// System.out.println("----Excle文件已生成------");
|
|
|
|
return wb; |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception { |
|
|
|
InputStream inputStream = new FileInputStream("D:\\1.xlsx"); |
|
|
|
Workbook workbook = new XSSFWorkbook(inputStream); |
|
|
|
XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0); |
|
|
|
XSSFRow row = sheet.getRow(6); |
|
|
|
XSSFCell newCell = row.createCell(2); |
|
|
|
XSSFCell newCell1 = row.createCell(3); |
|
|
|
//添加公式
|
|
|
|
newCell.setCellFormula("A7+B7"); |
|
|
|
newCell1.setCellFormula("A8/B8"); |
|
|
|
//设置打印区域
|
|
|
|
workbook.setPrintArea( |
|
|
|
0, //工作薄 下标0开始
|
|
|
|
0, //起始列 下标0开始
|
|
|
|
20, //终止列 下标0开始
|
|
|
|
0, //起始行 下标0开始
|
|
|
|
20 //终止行 下标0开始
|
|
|
|
); |
|
|
|
// CellStyle style = workbook.createCellStyle();
|
|
|
|
// style.setFillBackgroundColor();
|
|
|
|
|
|
|
|
OutputStream stream = new FileOutputStream(new File("D:\\1.xlsx")); |
|
|
|
workbook.write(stream); |
|
|
|
stream.close(); |
|
|
|
|
|
|
|
// List<List<PoiUtilCell>> list = new ArrayList<>();
|
|
|
|
// List<PoiUtilCell> cells = new ArrayList<>();
|
|
|
|
// cells.add(new PoiUtilCell("1", 2, 1));
|
|
|
|
// cells.add(new PoiUtilCell());
|
|
|
|
// cells.add(new PoiUtilCell());
|
|
|
|
// cells.add(new PoiUtilCell());
|
|
|
|
// cells.add(new PoiUtilCell());
|
|
|
|
// cells.add(new PoiUtilCell("2"));
|
|
|
|
// list.add(cells);
|
|
|
|
// List<PoiUtilCell> cells2 = new ArrayList<>();
|
|
|
|
// cells2.add(new PoiUtilCell("3", 1, 2));
|
|
|
|
// cells2.add(new PoiUtilCell("4"));
|
|
|
|
// cells2.add(new PoiUtilCell("",3,1));
|
|
|
|
// list.add(cells2);
|
|
|
|
// List<PoiUtilCell> cells3 = new ArrayList<>();
|
|
|
|
// cells3.add(new PoiUtilCell("5"));
|
|
|
|
// cells3.add(new PoiUtilCell("6"));
|
|
|
|
// list.add(cells3);
|
|
|
|
// List<PoiUtilCell> cells4 = new ArrayList<>();
|
|
|
|
// list.add(cells4);
|
|
|
|
// List<PoiUtilCell> cells5 = new ArrayList<>();
|
|
|
|
// cells5.add(new PoiUtilCell("9", 2, 2));
|
|
|
|
// cells5.add(new PoiUtilCell("9"));
|
|
|
|
// list.add(cells5);
|
|
|
|
|
|
|
|
// Workbook wbs = exportWB("wbs", list, workbook);
|
|
|
|
// OutputStream stream = new FileOutputStream(new File("D:\\1.xlsx"));
|
|
|
|
// wbs.write(stream);
|
|
|
|
// stream.close();
|
|
|
|
|
|
|
|
// setImg("C:\\Users\\逗\\Desktop\\0cf3ffd8f0001c4310b02f706753721.png",1,2,1,2);
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 获取excel的列号 |
|
|
|
* @param num 第几列(从1开始) |
|
|
|
* @return |
|
|
|
* @return 返回列号 |
|
|
|
*/ |
|
|
|
public static String toRadix(Integer num) throws Exception{ |
|
|
|
String[] array = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; |
|
|
@ -579,9 +524,63 @@ public class PoiUtil { |
|
|
|
out = array[num - 1]; |
|
|
|
} |
|
|
|
return out; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception { |
|
|
|
InputStream inputStream = new FileInputStream("C:\\Users\\逗\\Desktop\\新建文件夹\\1.xlsx"); |
|
|
|
Workbook workbook = new XSSFWorkbook(inputStream); |
|
|
|
// XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0);
|
|
|
|
// XSSFRow row = sheet.getRow(6);
|
|
|
|
// XSSFCell newCell = row.createCell(2);
|
|
|
|
// XSSFCell newCell1 = row.createCell(3);
|
|
|
|
// //添加公式
|
|
|
|
// newCell.setCellFormula("A7+B7");
|
|
|
|
// newCell1.setCellFormula("A8/B8");
|
|
|
|
// //设置打印区域
|
|
|
|
// workbook.setPrintArea(
|
|
|
|
// 0, //工作薄 下标0开始
|
|
|
|
// 0, //起始列 下标0开始
|
|
|
|
// 20, //终止列 下标0开始
|
|
|
|
// 0, //起始行 下标0开始
|
|
|
|
// 20 //终止行 下标0开始
|
|
|
|
// );
|
|
|
|
//// CellStyle style = workbook.createCellStyle();
|
|
|
|
//// style.setFillBackgroundColor();
|
|
|
|
//
|
|
|
|
// OutputStream stream = new FileOutputStream(new File("D:\\1.xlsx"));
|
|
|
|
// workbook.write(stream);
|
|
|
|
// stream.close();
|
|
|
|
List<List<PoiUtilCell>> list = new ArrayList<>(); |
|
|
|
|
|
|
|
List<PoiUtilCell> cells = new ArrayList<>(); |
|
|
|
cells.add(new PoiUtilCell("不跨行不跨列")); |
|
|
|
list.add(cells); |
|
|
|
|
|
|
|
List<PoiUtilCell> cells1 = new ArrayList<>(); |
|
|
|
cells1.add(new PoiUtilCell("两列一行", 2, 1)); |
|
|
|
list.add(cells1); |
|
|
|
|
|
|
|
List<PoiUtilCell> cells2 = new ArrayList<>(); |
|
|
|
cells2.add(new PoiUtilCell("一列两行", 1, 2)); |
|
|
|
list.add(cells2); |
|
|
|
// List<PoiUtilCell> cells3 = new ArrayList<>();
|
|
|
|
// cells3.add(new PoiUtilCell("5"));
|
|
|
|
// cells3.add(new PoiUtilCell("6"));
|
|
|
|
// list.add(cells3);
|
|
|
|
// List<PoiUtilCell> cells4 = new ArrayList<>();
|
|
|
|
// list.add(cells4);
|
|
|
|
// List<PoiUtilCell> cells5 = new ArrayList<>();
|
|
|
|
// cells5.add(new PoiUtilCell("9", 2, 2));
|
|
|
|
// cells5.add(new PoiUtilCell("9"));
|
|
|
|
// list.add(cells5);
|
|
|
|
|
|
|
|
Workbook wbs = exportWB("Sheet1", list, workbook); |
|
|
|
OutputStream stream = new FileOutputStream(new File("C:\\Users\\逗\\Desktop\\新建文件夹\\"+System.currentTimeMillis()+".xlsx")); |
|
|
|
wbs.write(stream); |
|
|
|
stream.close(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|