【java】+对excel进行操作_1_
一、
重点:https://www.jb51.net/article/110805.htm
public void writeData() throws IOException, BiffException { //打开文件 File file = new File("D:\\IDEA\\my\\测试文件\\工作簿1.xls"); Workbook wb = Workbook.getWorkbook(file); // WritableWorkbook book = Workbook.createWorkbook(file);//无wb表示将数据写到一个新文件中 WritableWorkbook book = Workbook.createWorkbook(file,wb);//有wb表示将数据写到原文件中,并不新建一个文件 WritableSheet sheet = book.getSheet(1); try { sheet.addCell(new Label(6, 6, "zhanggggg")); book.write(); book.close(); } catch (WriteException e) { e.printStackTrace(); } }
package com.zgx.excel; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import org.testng.annotations.Test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; /** * @author * @version 1.0 * @time 2019/5/24 16:35 */ public class Excel { /*** * 需求: * 1、根据某个单元格的内容定位出此单元格的位置 * 2、 */ InputStream inputStream; Workbook rwb; public void base() { /*** * 读取表,并创建workbook对象 * 备注:需要讲表格另存为xls格式才会读取成功,不能只改后缀名 */ { try { inputStream = new FileInputStream("表路径"); try { rwb = Workbook.getWorkbook(inputStream); } catch (IOException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } } @Test public void ttt() { base(); /*** *对表进行操作 */ //获取第一张sheet表 Sheet sheet1 = rwb.getSheet(0); //获取某个单元格的值 Cell cell1 = sheet1.getCell(0, 0); String cell1string = cell1.getContents().toString(); //根据内容查找单元格的位置 Cell cell2 = sheet1.findCell("T_G2_05"); String cell2string = cell2.getContents().toString(); int row = cell2.getRow(); int column = cell2.getColumn(); System.out.println("T_G2_05:" + row + "行," + column + "列。 内容:" + cell2string); //验证 System.out.println("值:" + cell1string); } }
二、java操作excel常用的两种方式(poi、jxl)
https://blog.csdn.net/ryan007liu/article/details/89357447
pol方式:
maven依赖:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
代码:
/** * 功能:拷贝sheet表 * * @param excelFilePath 原文件路径 * @param rootSheetName 原sheet表名称 * @param newSheetNames 新sheet表名称 * @param newData 新sheet表需要改变的数据(null 表示 复制后数据不变) * @param outxlsxFilePath 新文件路径 */ public static void copySheet(String excelFilePath, String rootSheetName, List<String> newSheetNames, List<String> newData, String outxlsxFilePath) { try { workbook = new XSSFWorkbook(new FileInputStream(excelFilePath)); // 读取源sheet表中的数据 rootSheet = workbook.getSheet(rootSheetName); int lastRowNum = rootSheet.getLastRowNum(); // 总行数 int lastCellNum = rootSheet.getRow(0).getPhysicalNumberOfCells(); // 总列数 // 创建新的sheet表 并把源sheet表中的数据复制进去 XSSFSheet rootSheetPoint = workbook.getSheet(rootSheetName); int rootSheetIndex = workbook.getSheetIndex(rootSheetPoint); for (int i = 0; i < newSheetNames.size(); i++) { // 复制原sheet到新sheet 并修改新sheet表的名称 XSSFSheet copySheet = workbook.cloneSheet(rootSheetIndex); String copySheetName = copySheet.getSheetName(); int copySheetIndex = workbook.getSheetIndex(copySheetName); workbook.setSheetName(copySheetIndex, newSheetNames.get(i)); if (newData != null) { // 给第1行第0列的所有sheet表赋值(根据具体业务) copySheet.getRow(1).getCell(0).setCellValue(newData.get(i)); } } workbook.write(new FileOutputStream(outxlsxFilePath)); } catch (IOException e) { e.printStackTrace(); } }
如果忍耐算是坚强 我选择抵抗 如果妥协算是努力 我选择争取
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义