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

java-poi操作笔记

row表示行,cell表示row中的第几个cell

package sample.Utils;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;public  class excelUtils {//读取excelpublic static Workbook readExcel(String filePath){Workbook wb = null;if(filePath==null){return null;}String extString = filePath.substring(filePath.lastIndexOf("."));InputStream is = null;try {is = new FileInputStream(filePath);if(".xls".equals(extString)){return wb = new HSSFWorkbook(is);}else if(".xlsx".equals(extString)){return wb = new XSSFWorkbook(is);}else{return wb = null;}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return wb;}public static Object getCellFormatValue(Cell cell){Object cellValue = null;if(cell!=null){//判断cell类型switch(cell.getCellType()){case Cell.CELL_TYPE_NUMERIC:{cellValue = String.valueOf(cell.getNumericCellValue());break;}case Cell.CELL_TYPE_FORMULA:{//判断cell是否为日期格式if(DateUtil.isCellDateFormatted(cell)){//转换为日期格式YYYY-mm-ddcellValue = cell.getDateCellValue();}else{//数字cellValue = String.valueOf(cell.getNumericCellValue());}break;}case Cell.CELL_TYPE_STRING:{cellValue = cell.getRichStringCellValue().getString();break;}default:cellValue = "";}}else{cellValue = "";}return cellValue;}public static void writeExcel(String filePath,String sheet1Name,int row1,int one,int two,int three,int four, String onecontent,String twocontent,String threecontent,String fourcontent){FileInputStream inputStream = null;try {inputStream = new FileInputStream(filePath);} catch (FileNotFoundException e) {e.printStackTrace();}Workbook workbook = null;try {workbook = new XSSFWorkbook(inputStream);} catch (IOException e) {e.printStackTrace();}Sheet sheet = workbook.getSheet(sheet1Name);Row row = sheet.createRow(row1);Cell cell1 =  row.createCell(one);cell1.setCellValue(onecontent);Cell cell24 =  row.createCell(two);cell24.setCellValue(twocontent);Cell cell25 =  row.createCell(three);cell25.setCellValue(threecontent);Cell cell26 =  row.createCell(four);cell26.setCellValue(fourcontent);// 写入数据try (FileOutputStream outputStream = new FileOutputStream(filePath)) {workbook.write(outputStream);} catch (IOException e) {e.printStackTrace();} finally {try {if (workbook != null) {workbook.close();}} catch (IOException e) {e.printStackTrace();}}}}
http://www.lryc.cn/news/253932.html

相关文章:

  • PHP:js中怎么使用PHP变量,php变量为数组时的处理
  • 网工学习7-配置 GVRP 协议
  • python:格式化输出指定内容
  • 【C语言】7-35 强迫症 分数 10
  • 如何快速了解一家公司?
  • ZPLPrinter Emulator SDK for .NET 6.0.23.1123​ Crack
  • 查收查引(通过文献检索开具论文收录或引用的检索证明)
  • Python一帮一
  • 学员追访 | “IC的标签并不是只有高薪与965”
  • LeetCode(39)赎金信【哈希表】【简单】
  • 输入日期求n天后
  • 科技论文中的Assumption、Remark、Property、Lemma、Theorem、Proof含义
  • 逆向爬虫进阶实战:突破反爬虫机制,实现数据抓取
  • 个体民营诊所-如何打破传统发展瓶颈,增客流-提营收?
  • Filament引擎分析--command抽象设备API
  • 网络协议与响应码
  • 彻底删除VsCode配置和安装过的插件与缓存
  • 【XILINX】ISE chipscope出现错误 Can‘t load jre bin client jvm.dll
  • 并发编程(已整理,已废弃)
  • 第一篇:MongoDB的安装、启动、关闭、链接shell
  • Python爬虫之重放攻击详解
  • 构建一个语音转文字的WebApi服务
  • 在开发软件KEIL MDK和IAR开发工程里面打印行号、文件名、函数名、时间
  • springboot(ssm健身器材用品网 健身用品商城Java(codeLW)
  • idea git合并推送分支
  • AntDesignBlazor示例——创建列表页
  • 微信小程序模板选择指南:如何找到靠谱的平台?
  • es常用查询编辑
  • 记录 | linux静态库和动态库的理解
  • ParBFT: Faster Asynchronous BFT Consensus with a Parallel Optimistic Path