需要导入的jar包下载地址https://pan.baidu.com/s/16cTpUfx0KvKkbGYkXAUKMA
代码:ReadExcel.java
//信1605-3 20163432 张运涛 package domain; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import jxl.Cell; import jxl.CellType; import jxl.Sheet; import jxl.Workbook; import jxl.write.Label; public class ReadExcel { public static void main(String[] args) { jxl.Workbook readwb = null; try { InputStream instream = new FileInputStream("C:\\Users\\lenovo\\Desktop\\测试.xls"); //制定本地excel的文件目录 readwb = Workbook.getWorkbook(instream); //Sheet的下标是从0开始 //获取第一张Sheet表 Sheet readsheet = readwb.getSheet(0); //获取Sheet表中所包含的总列数 int rsColumns = readsheet.getColumns(); //获取Sheet表中所包含的总行数 int rsRows = readsheet.getRows(); //获取指定单元格的对象引用 for (int i = 0; i < rsRows; i++) { for (int j = 0; j < rsColumns; j++) { Cell cell = readsheet.getCell(j, i); System.out.print(cell.getContents() + " "); } System.out.println("数据读取结束"); } //利用已经创建的Excel工作薄,创建新的可写入的Excel工作薄 jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File( "C:\\Users\\lenovo\\Desktop\\测试写入.xls"), readwb); //读取第一张工作表 jxl.write.WritableSheet ws = wwb.getSheet(0); //获得第一个单元格对象 jxl.write.WritableCell wc = ws.getWritableCell(10, 5); //判断单元格的类型, 做出相应的转化 if (wc.getType() == CellType.LABEL) { Label l = (Label) wc; l.setString("新增加的内容"); } //写入Excel对象 wwb.write(); wwb.close(); } catch (Exception e) { e.printStackTrace(); } finally { readwb.close(); } } }
运行效果
原文地址:https://www.cnblogs.com/zyt-bg/p/9961012.html
时间: 2024-10-03 14:47:27