JXL读取,写入Excel

JXL读取,写入Excel2003

相关阅读:
poi 读写excel2003:http://www.cnblogs.com/gavinYang/p/3576739.html
poi 读写excel2007:http://www.cnblogs.com/gavinYang/p/3576741.html

package com.write;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
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 JSXWriteExcelXls {

    public static void main(String[] args) {

        //只支持读取2003
        
        Map<Integer, List<String[]>> map = readExcel(new File("e:/读取excel.xls")); 
        for(int n=0;n<map.size();n++){
            List<String[]> list = map.get(n);
            System.out.println("-------------------------sheet"+n+"--------------------------------");
            for(int i=0;i<list.size();i++){
                String[] arr = (String[]) list.get(i);
                for(int j=0;j<arr.length;j++){
                    if(j==arr.length-1)
                        System.out.print(arr[j]);
                    else
                        System.out.print(arr[j]+"|");
                }
                System.out.println();
            }
        }
        
        writeExcel(new File("e:/写入excel.xls"),map);
    }

    public static Map<Integer, List<String[]>> readExcel(File file) {
        Map<Integer, List<String[]>> map = new HashMap<Integer, List<String[]>>();
        try {
            Workbook wb = Workbook.getWorkbook(file);
            for(int n=0;n<wb.getSheets().length;n++){
                Sheet sheet = wb.getSheet(n);
                if (sheet == null) {
                    continue;
                }
                List<String[]> list = new ArrayList<String[]>();
                for (int i = 0; i < sheet.getRows(); i++) {
                    Cell[] row = sheet.getRow(i);
                    if (row == null) {
                        continue;
                    }
                    String[] singleRow = new String[row.length];
                    for (int j = 0; j < sheet.getColumns(); j++) {
                        Cell cell = sheet.getCell(j, i); // 列 行
                        //获取Cell的类型:cell.getType()   类型的枚举CellType.xxx
                        singleRow[j] = cell.getContents();
                    }
                    list.add(singleRow);
                }
                map.put(n, list);
            }
        } catch (BiffException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return map;
    }

   // 写文件
    public static void writeExcel(File file,Map<Integer, List<String[]>> map) {
        try {
            // 创建文件
            WritableWorkbook book = Workbook.createWorkbook(file);
            for(int n=0;n<map.size();n++){
                WritableSheet sheet = book.createSheet("sheet"+(n+1), n);
                List<String[]> list = map.get(n);
                for(int i=0;i<list.size();i++){
                    String[] arr = list.get(i);
                    for(int j=0;j<arr.length;j++){
                        sheet.addCell(new Label(j, i, arr[j]));
                    }
                }
            }
            book.write();
            book.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/gavinYang/p/3576819.html