使用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);
- }
- }