当前位置: 首页 > news >正文

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;}

http://www.dtcms.com/a/306976.html

相关文章:

  • OpenWrt Network configuration
  • 百度统计在哪里添加网站?
  • Linux系统启动不受未挂载硬盘影响的解决方案
  • Windows系统使用命令生成文件夹下项目目录树(文件结构树)的两种高效方法
  • 深度学习-丢弃法 Dropout
  • C语言基础11——结构体1
  • Qt Quick 动画与过渡效果
  • QT中QTableView+Model+Delegate实现一个demo
  • TikTok 视频审核模型:用逻辑回归找出特殊类型的视频
  • 全栈:SSH和SSM和Springboot mybatisplus有什么区别?
  • 以ros的docker镜像为例,探讨docker镜像的使用
  • 力扣刷题日常(7-8)
  • 【Arch-Linux,hyprland】常用配置-已实验成功指令大全(自用)(持续更新)
  • 如何保证数据库的持久性与一致性:从 Linux 磁盘缓存策略到 MySQL 的设计
  • 执业药师证识别技术:医药健康生态中发挥愈发关键的作用
  • 微软:科技领域的创新巨头
  • Sleeping Cup 论坛:连接开发者与创新的桥梁
  • 隧道COVI检测器的用处
  • [SKE]使用OpenSSL库实现AES、SM4、DES、RSA、3DES_EDE和3DES_EEE算法的加解密验证
  • SringBoot入门
  • Linux启动防火墙提示提示 Active: failed (Result: timeout)
  • Golang 指针与引用深度解析:对比 C/C++ 的内存管理哲学
  • Jupyter Notebook安装使用
  • Javascript对象合并
  • Centos7 | 防火墙(firewalld)使用ipset管理ip地址的集合
  • MySQL 读写分离(含示例代码)
  • 新注册企业信息查询“数据大集网”:驱动企业增长的源头活水
  • 10 卷积神经网络
  • LLMs之Agent:GLM-4.5的简介、安装和使用方法、案例应用之详细攻略
  • 51单片机入门:数码管原理介绍及C代码实现