MCPcopy Create free account
hub / github.com/LisenCoding/cangku / PoiUtil

Class PoiUtil

boot/src/main/java/com/utils/PoiUtil.java:18–109  ·  view source on GitHub ↗

文件导入到处

Source from the content-addressed store, hash-verified

16 * 文件导入到处
17 */
18public class PoiUtil {
19 /**
20 * 导入
21 *
22 * @param url
23 * @return
24 * @throws Exception
25 */
26 public static List<List<String>> poiImport(String url) throws Exception {
27 List<List<String>> list = new ArrayList<>();
28 // 创建Excel 读取文件内容
29 HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(new File(url)));
30 /**
31 * 第一种方式读取Sheet页
32 */
33// HSSFSheet sheet = workbook.getSheet("Sheet0");
34 /**
35 * 第二种方式读取Sheet页
36 */
37 HSSFSheet sheet = workbook.getSheetAt(0);//获取工作表
38 for (int i = 0; i < sheet.getLastRowNum()+1; i++) {
39 HSSFRow row = sheet.getRow(i);//获取行
40 List<String> rowlist = new ArrayList<>();//行数据
41 for (int j = 0; j < row.getLastCellNum(); j++) {
42 HSSFCell cell = row.getCell(j);
43 cell.setCellType(Cell.CELL_TYPE_STRING);
44 String value = cell.getStringCellValue();
45 rowlist.add(value);//行中数据添加到行中
46 }
47 list.add(rowlist);//将行数据添加到list中
48 }
49 return list;
50 }
51
52
53
54
55
56
57 // 导出
58 public static void poiExport(List<List<String>> list, String url) throws Exception {
59 //创建Excel工作薄
60 HSSFWorkbook workbook = new HSSFWorkbook();
61 //创建一个工作表shheet
62 HSSFSheet sheet = workbook.createSheet();
63 for (int i = 0; i < list.size(); i++) {
64 HSSFRow row = sheet.createRow(i);
65 List<String> dataList = list.get(i);
66 for (int j = 0; j < dataList.size(); j++) {
67 HSSFCell cell = row.createCell(j);
68 cell.setCellValue(dataList.get(j));
69 }
70
71 }
72 FileOutputStream stream = FileUtils.openOutputStream(new File(url));
73 workbook.write(stream);
74 stream.close();
75 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected