2025年3月23日 星期日 甲辰(龙)年 月廿二 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Java

jxl实现java读写excel

时间:06-28来源:作者:点击数:63

使用jxl对excel文件进行简单的读写操作。

  • // 读excel文件中的数据在控制台输出
  • public class readExcel {
  • public static void main(String[] args) throws IOException, BiffException {
  • // 1.获取工作簿
  • File file = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\practise.xls");
  • Workbook wb = Workbook.getWorkbook(file);
  • // 2.获取sheet页
  • Sheet sheet = wb.getSheet("student");
  • // 3.获取单元格数据
  • Cell cell = null;
  • // getRows获取行数 getColumns获取列数
  • for (int i = 0; i < sheet.getRows(); i++) {
  • for (int j = 0; j < sheet.getColumns(); j++) {
  • cell = sheet.getCell(j, i);
  • System.out.print(cell.getContents() + "\t");
  • }
  • System.out.println("");
  • }
  • }
  • }
  • //将数据写入excel文件中
  • public class writeExcel {
  • public static void main(String[] args) throws IOException, BiffException, RowsExceededException, WriteException {
  • //定位到user文件,不存在时创建一个
  • File file = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\user.xls");
  • if (!file.exists()) {
  • file.createNewFile();
  • }
  • // 1.创建工作簿
  • WritableWorkbook wb = Workbook.createWorkbook(file);
  • // 2.创建sheet页
  • WritableSheet sheet = wb.createSheet("student", 0);
  • // 3.创建单元格并写入
  • Label label1 = new Label(0, 0, "id");
  • Label label2 = new Label(1, 0, "name");
  • Label label3 = new Label(2, 0, "score");
  • sheet.addCell(label1);
  • sheet.addCell(label2);
  • sheet.addCell(label3);
  • }
  • }

 

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门