今天 群里面有个美眉 需要解析 excel 文件, 在网上搜索了以下 , 挺简单的 。
下面代码:
首先需要:jxl.jar
实现:
"public static void main(String[] args) throws BiffException, IOException {
InputStream is = new FileInputStream("D:\\YC50学员名单.xls"); // 获取 一个 输入流 , 文件路径为D:\\YC50学员名单.xls
try {
Workbook wb = Workbook.getWorkbook(is); //获取文件的Workbook 对象
int wbNum = wb.getNumberOfSheets(); //
for (int i = 0; i < wbNum; i++) {
Sheet sheet = wb.getSheet(i); //集合
String sheetName = sheet.getName(); //获取标题名称
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("sheetName=" + sheetName);
if (sheet != null) { //如果集合不为空的话
// 获取表格总列数
int rsColumns = sheet.getColumns();
// 获取表格总行数
int rsRows = sheet.getRows();
// 循环文件里的数据
List<Student> students = new ArrayList<Student>();// 你需要获取的List<Student>
for (int j = 0; j < rsRows; j++) {
Cell[] cells = sheet.getRow(j); // 这里获取的是一个model 就是以行的值
for (int k = 0; k < rsColumns; k++) {
Student student = new Student();
student.setId(cells[k].getContents());
student.setName(cells[k].getContents());
student.setSex(cells[k].getContents());
student.setPhoneNum(cells[k].getContents());
student.setQqNum(cells[k].getContents());
student.setSchool(cells[k].getContents());
student.setSleepRoom(cells[k].getContents());
students.add(student);
}
}
for (Student student : students) {
System.out.println(student.getId());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
"
另附: 应该不是 API 。。
代表 WPS表格 工作簿。Workbook 对象是 Workbooks 集合的成员。
Class Workbook
属性列表
属性 | 描述 |
---|---|
ActiveSheet | 该属性返回指定工作簿中的活动工作表。Worksheet 类型,只读。 |
BuiltinDocumentProperties | 该属性返回一个 DocumentProperties 集合,该集合代表指定工作簿的所有内置文档属性。DocumentProperties 类型,只读。 |
Colors | 该属性返回或设置指定工作簿调色板中的颜色。Variant 类型,可读写。 |
CustomDocumentProperties | 该属性返回 DocumentProperties 集合,该集合代表指定工作簿的所有自定义文档属性。DocumentProperties 类型,只读。 |
ExtraColors | 该属性返回指定工作簿中可用的其他颜色。ExtraColors 类型,只读。 |
FullName | 该属性返回指定工作簿的名称(包括其磁盘路径的字符串)。String 类型,只读。 |
HasPassword | 该属性返回指定工作簿是否有密码保护。Boolean 类型,只读。 |
Name | 该属性返回指定工作簿的名称。String 类型,只读。 |
Names | 该属性返回 Names 集合,此集合代表指定工作簿中的所有名称(包括所有带工作表区分符的名称)。Names 类型,只读。 |
PasswordEncryptionAlgorithm | 该属性返回 ET 应用程序对指定的工作簿编写密码时使用的算法。String 类型,只读。 |
PasswordEncryptionFileProperties | 该属性返回 ET 应用程序是否对具有密码保护的指定工作簿的文件属性进行加密。Boolean 类型,只读。 |
PasswordEncryptionKeyLength | 该属性返回对指定的工作簿编写密码时 ET 应用程序使用的算法的关键字长度。Long 类型,只读。 |
PasswordEncryptionProvider | 该属性返回对指定的工作簿编写密码时 ET 应用程序使用的算法加密提供程序的名称。String 类型,只读。 |
Path | 该属性返回指定工作簿完整的路径(不包括工作簿名称)。String 类型,只读。 |
ProtectStructure | 该属性返回指定工作簿中工作表结构是否受保护。Boolean 类型,只读。 |
ProtectWindows | 该属性返回指定工作簿窗口是否受保护。Boolean 类型,只读。 |
Saved | 该属性返回指定的工作簿是否发生过更改。Boolean 类型,可读写。 |
Sheets | 该属性返回指定工作簿中的所有工作表。Sheets 类型,只读。 |
Styles | 该属性返回指定工作簿中的所有样式。Styles 类型,只读。 |
Windows | 该属性返回指定工作簿中的所有窗口。Windows 类型,只读。 |
Worksheets | 该属性返回指定工作簿中的所有工作表。Sheets 类型,只读。 |
方法列表
方法 | 描述 |
---|---|
Close | 该方法用于关闭指定的工作簿。 |
DeleteNumberFormat | 该方法用于从指定工作簿中删除一个自定义数字格式。 |
PrintOut | 该方法用于打印工作簿。 |
Protect | 该方法用于保护工作簿使其不被修改。 |
Save | 该方法用于保存指定工作簿所做的更改。 |
SaveAs | 该方法用于另存为工作表。 |
SendMail | 该方法用于使用已安装的邮件系统发送工作簿。 |
Unprotect | 该方法用于取消指定工作簿的保护。 |