(1)Java读取excel表格
package com.songyan.excel; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class ReadExcel { public static void main(String[] args) { ReadExcel obj = new ReadExcel(); // 此处为我创建Excel路径:E:/zhanhj/studysrc/jxl下 File file = new File("D:/啊.xls"); List excelList = obj.readExcel(file); System.out.println(obj.readExcel2(file)); /* System.out.println("list中的数据打印出来"); for (int i = 0; i < excelList.size(); i++) { List list = (List) excelList.get(i); for (int j = 0; j < list.size(); j++) { System.out.print(list.get(j)); } System.out.println(); }*/ } // 去读Excel的方法readExcel,该方法的入口参数为一个File对象 public String readExcel2(File file) { String res = ""; try { // 创建输入流,读取Excel InputStream is = new FileInputStream(file.getAbsolutePath()); // jxl提供的Workbook类 Workbook wb = Workbook.getWorkbook(is); // Excel的页签数量 int sheet_size = wb.getNumberOfSheets(); for (int index = 0; index < sheet_size; index++) { List<List> outerList=new ArrayList<List>(); // 每个页签创建一个Sheet对象 Sheet sheet = wb.getSheet(index); // sheet.getRows()返回该页的总行数 for (int i = 0; i < sheet.getRows(); i++) { List innerList=new ArrayList(); // sheet.getColumns()返回该页的总列数 for (int j = 0; j < sheet.getColumns(); j++) { String cellinfo = sheet.getCell(j, i).getContents(); if(cellinfo.isEmpty()){ continue; } innerList.add(cellinfo); res = res + cellinfo + ","; } outerList.add(i, innerList); } return res.substring(4,res.length()-1); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } // 去读Excel的方法readExcel,该方法的入口参数为一个File对象 public List readExcel(File file) { try { // 创建输入流,读取Excel InputStream is = new FileInputStream(file.getAbsolutePath()); // jxl提供的Workbook类 Workbook wb = Workbook.getWorkbook(is); // Excel的页签数量 int sheet_size = wb.getNumberOfSheets(); for (int index = 0; index < sheet_size; index++) { List<List> outerList=new ArrayList<List>(); // 每个页签创建一个Sheet对象 Sheet sheet = wb.getSheet(index); // sheet.getRows()返回该页的总行数 for (int i = 0; i < sheet.getRows(); i++) { List innerList=new ArrayList(); // sheet.getColumns()返回该页的总列数 for (int j = 0; j < sheet.getColumns(); j++) { String cellinfo = sheet.getCell(j, i).getContents(); if(cellinfo.isEmpty()){ continue; } innerList.add(cellinfo); /*System.out.print(cellinfo);*/ } outerList.add(i, innerList); /* System.out.println();*/ } return outerList; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }
read && console:
(2)生成excel&&写入表格
package com.songyan.excel; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class ReadExcel { public static void main(String[] args) { ReadExcel.writeExcel("D:\11.xls"); } public static void writeExcel(String fileName){ WritableWorkbook wwb = null; try { // 创建一个可写入的工作簿(WorkBook)对象, //这里用父类方法createWorkbook创建子类WritableWorkbook让我想起了工厂方法 wwb = Workbook.createWorkbook(new File(fileName)); // 创建一个可写入的工作表 // Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作簿中的位置 WritableSheet ws = wwb.createSheet("sheetTest", 0); Label labelC = new Label(0,0,"专利号"); Label labeld = new Label(0,1,"例:CN201621266091.4"); Label labele = new Label(0,2,"例:CN201621266091.6"); ws.addCell(labelC); ws.addCell(labeld); ws.addCell(labele); wwb.write();// 从内从中写入文件中 wwb.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("生成第一个Excel文件"+fileName+"成功"); } }
console && 生成文件