Excel文件解析
POI
//创建工作簿 XSSFWorkbook workbook = new XSSFWorkbook();//创建工作表 XSSFSheet sheet=workbook.createSheet("Sheet1");//创建行 XSSFRow row=sheet.createRow(0);//根据行创建单元格 XSSFCell cell=row.createCell(0); XSSFCell cell2=row.createCell(1);//填充值 cell.setCellValue("Hello World"); cell2.setCellValue("Hello Java");try(BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream("D:\\IOTest\\test.xlsx"))){workbook.write(outputStream); }catch (IOException e){e.printStackTrace(); }finally {workbook.close(); } ;
//创建工作簿XSSFWorkbook workbook = new XSSFWorkbook();//创建工作表XSSFSheet sheet=workbook.createSheet("Sheet1");//创建行XSSFRow row=sheet.createRow(0);//根据行创建单元格XSSFCell cell1=row.createCell(0);XSSFCell cell2=row.createCell(1);XSSFCell cell3=row.createCell(2);XSSFCell cell4=row.createCell(3);cell1.setCellValue("序号");cell2.setCellValue("优惠码");cell3.setCellValue("校验码");cell4.setCellValue("创建日期");DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");for(int i=1;i<=1000;i++){row=sheet.createRow(i);cell1=row.createCell(0);cell2=row.createCell(1);cell3=row.createCell(2);cell4=row.createCell(3);cell1.setCellValue(String.valueOf(i));cell2.setCellValue(UUID.randomUUID().toString().substring(0,8));cell3.setCellValue(UUID.randomUUID().toString().substring(0,4));cell4.setCellValue(LocalDateTime.now().format(formatter));}try(BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream("D:\\IOTest\\test2.xlsx"))){workbook.write(outputStream);}catch (IOException e){e.printStackTrace();}finally {workbook.close();}
try (//创建工作簿XSSFWorkbook workbook = new XSSFWorkbook();BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream("D:\\IOTest\\test3.xlsx"))){//创建单元格表头样式XSSFCellStyle headerStyle = workbook.createCellStyle();//设置单元格的水平对齐类型为居中对齐headerStyle.setAlignment(HorizontalAlignment.CENTER);//设置单元格的垂直对齐方式为居中对齐headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置字体样式Font headerFont = workbook.createFont();headerFont.setBold(true); //是否加粗headerFont.setColor(Font.COLOR_RED); //颜色设置为红色headerStyle.setFont(headerFont);//创建工作表XSSFSheet sheet=workbook.createSheet("礼品金额表");//创建行Row row=sheet.createRow(0);//根据行创建单元格Cell cell1=row.createCell(0);cell1.setCellStyle(headerStyle);//设置单元格样式cell1.setCellValue("序号"); //设置单元格的值Cell cell2=row.createCell(1);cell2.setCellStyle(headerStyle);cell2.setCellValue("礼品金额");Cell cell3=row.createCell(2);cell3.setCellStyle(headerStyle);cell3.setCellValue("时间");Random random=new Random();//创建单元格样式XSSFCellStyle moneyStyle=workbook.createCellStyle();XSSFCellStyle dateStyle=workbook.createCellStyle();//设置样式moneyStyle.setAlignment(HorizontalAlignment.LEFT);dateStyle.setAlignment(HorizontalAlignment.LEFT);//创建数据格式对象DataFormat moneyFormat = workbook.createDataFormat();//给数据格式样式对象设置样式short shortMoney=moneyFormat.getFormat("¥###,#");DataFormat dateFormat = workbook.createDataFormat();short shortDate=moneyFormat.getFormat("yyyy年MM月dd日");//给单元格设置数据样式moneyStyle.setDataFormat(shortMoney);dateStyle.setDataFormat(shortDate);for(int i=1;i<=1000;i++){Row r=sheet.createRow(i);Cell c1=r.createCell(0);c1.setCellValue(i);Cell c2=r.createCell(1);c2.setCellStyle(moneyStyle);c2.setCellValue(random.nextInt(1000000));Cell c3=r.createCell(2);c3.setCellStyle(dateStyle);c3.setCellValue(new Date());}workbook.write(outputStream);System.out.println("程序结束");}catch (IOException e){e.printStackTrace(); }
try (//加载,将输入流传递给Excel对象BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\IOTest\\test2.xlsx"));XSSFWorkbook wb = new XSSFWorkbook(bis)){//获取工作表XSSFSheet sheet = wb.getSheetAt(0);//获取行for(Row row : sheet){//通过获取单元格for(Cell cell : row){System.out.print(cell.getStringCellValue() + " ");}System.out.println();}}catch (IOException e){e.printStackTrace();}
try (//加载,将输入流传递给Excel对象BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\IOTest\\test2.xlsx"));XSSFWorkbook wb = new XSSFWorkbook(bis)) {//获取工作表的个数int numSheets = wb.getNumberOfSheets();System.out.println("Number of Sheets: " + numSheets);//获取工作表XSSFSheet sheet = wb.getSheet("Sheet1");System.out.println(sheet);System.out.println("总行数为:"+sheet.getPhysicalNumberOfRows());for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {//通过工作表获取工作行对象Row row = sheet.getRow(i);
// System.out.println("第"+i+"行一共有"+row.getPhysicalNumberOfCells()+"列");for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {Cell cell = row.getCell(j);//获取单元格的值,单元格的值的类型if(cell.getCellType()== CellType.NUMERIC){System.out.print(cell.getNumericCellValue()+" ");}else if(cell.getCellType()== CellType.STRING){System.out.print(cell.getStringCellValue()+" ");}}System.out.println();}}catch (IOException e){e.printStackTrace();}
//使用SXSSFWorkbook进行设置的最大行数try(Workbook wb = new SXSSFWorkbook(100);FileOutputStream fos = new FileOutputStream("D:\\IOTest\\big.xlsx");){Sheet sheet=wb.createSheet("大文本");for(int i=0;i<100000; i++){Row row=sheet.createRow(i);row.createCell(0).setCellValue(i);row.createCell(1).setCellValue(UUID.randomUUID().toString().substring(0,4));row.createCell(2).setCellValue(Math.random()*10000);}wb.write(fos);}catch (IOException e) {e.printStackTrace();}
easyExcel
public static void main(String[] args) {//写出
// writeExcel("D:\\IOTest\\easyExcel.xlsx");//读入readExcel("D:\\IOTest\\easyExcel.xlsx");}private static void readExcel(String s) {try(BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"))) {//参数1:读取表格的路径//参数2:映射的实体类//参数3:读取到数据的操作是什么EasyExcel.read(s, Data.class, new AnalysisEventListener<Data>() {//读取到此行数据做的操作@Overridepublic void invoke(Data data, AnalysisContext analysisContext) {try {if(data.getUuid().contains("0")){bw.write(data.getUuid());bw.newLine();}}catch (Exception e){e.printStackTrace();}}//读取完数据做的操作,关闭写出资源@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {try {bw.close();}catch (Exception e){e.printStackTrace();}System.out.println("读取分析结束");}}).sheet().doRead();}catch (IOException e){e.printStackTrace();}}private static void writeExcel(String s) {//参数1:写出的路径,参数2:写出时参考的实体类EasyExcel.write(s, Data.class).sheet(). //写到哪个表中doWrite(dataReady()); //执行写出操作,数据}private static List<Data> dataReady() {List<Data> list = new ArrayList<>();for (int i = 1; i < 100000; i++) {Data data = new Data(String.valueOf(i), UUID.randomUUID().toString().substring(0,8), new Date());list.add(data);}return list;}