jxl 读取xls,并转为二维数组可进行保存

jxl.jar: 

通过java操作excel表格的工具类库
支持Excel 95-2000的所有版本
生成Excel 2000标准格式
支持字体、数字、日期操作
能够修饰单元格属性
支持图像和图表
应该说以上功能已经能够大致满足我们的需要。最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。另外需要说明的是,这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。

  直接上代码:jxl操作xls其实很简单

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.InputStream;
 4 
 5 import jxl.Cell;
 6 import jxl.CellType;
 7 import jxl.DateCell;
 8 import jxl.LabelCell;
 9 import jxl.Sheet;
10 import jxl.Workbook;
11 
12 public class Jxl {
13     // 以下是导入excel的一系列属性
14     private static Sheet sheet;
15     private static String[][] excelValue;
16 
17     public static void main(String[] args) {
18         File upload = new File("D://1.xls");
19         if (upload.exists()) {
20             initExcel(upload); // 初始化
21             readExcel(); // 读取
22         } else {
23             System.out.println("file is not found");
24         }
25         System.out.println(excelValue[1][1]);// 输出验证下是否存入二维数组
26     }
27 
28     /**
29      * 读取excel文件中数据,保存到sheet对象中
30      * 
31      * @param upload
32      *            可以通用
33      */
34     private static void initExcel(File upload) {
35         Workbook rwb = null;
36         try {
37             InputStream is = new FileInputStream(upload);
38             rwb = Workbook.getWorkbook(is);
39             // 获得第一个工作表对象
40             sheet = rwb.getSheet(0);
41         } catch (Exception e) {
42             e.printStackTrace();
43         }
44     }
45 
46     /**
47      * 读取excel中数据进入excelValue数组中
48      * 
49      * 可以通用
50      */
51     private static void readExcel() {
52         excelValue = new String[sheet.getRows()][sheet.getColumns()]; // 将行和列存储到二维数组中
53         for (int i = 0; i < sheet.getRows(); i++)
54             for (int j = 0; j < sheet.getColumns(); j++) {
55                 Cell cell = sheet.getCell(j, i);// 将工作表分成一块一块
56                 if ("".equals(cell.getContents().toString().trim())) {
57                     excelValue[i][j] = "";
58                 }
59                 if (cell.getType() == CellType.LABEL) {
60                     LabelCell labelcell = (LabelCell) cell;
61                     excelValue[i][j] = labelcell.getString().trim();
62                 } else if (cell.getType() == CellType.NUMBER) {
63                     excelValue[i][j] = cell.getContents();
64                 } else if (cell.getType() == CellType.DATE) {
65                     DateCell datcell = (DateCell) cell;
66                     excelValue[i][j] = datcell.getDate().toString();
67                 } else {
68                     excelValue[i][j] = cell.getContents().toString().trim();
69                 }
70             }
71     }
72 
73 }
原文地址:https://www.cnblogs.com/Alandre/p/3359316.html