List的JavaExcel工具类

这也是大约在夏季,应该接近秋天了吧

支持office2003
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.io.File;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;

import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class RWExcel ...{

    public RWExcel() ...{
        // TODO Auto-generated constructor stub
    }

    public static void writerExcel(List list, List titel, String excelpath) ...{
        File f = null;
        Workbook wb = null;
        WritableWorkbook wwb = null;
        try ...{
            wwb = Workbook.createWorkbook(new File(excelpath));
            WritableSheet ws = wwb.createSheet("Label Formats", 0);

            for (int i = 0; i < titel.size(); i++) ...{
                ws.addCell(new Label(i, 0, titel.get(i).toString()));
            }
            int n = 1;
            for (int i = 0; i < list.size() - 1; i++) ...{
                List list1 = (List) list.get(i);
                for (int j = 0; j < list1.size(); j++) ...{
                    Label label = new Label(j, n, list1.get(j).toString());
                    ws.addCell(label);
                }
                n++;
            }

        } catch (Exception e) ...{
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally ...{
            if (wwb != null) ...{
                try ...{
                    wwb.write();
                    wwb.close();
                } catch (Exception e) ...{
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }

    }

    public static List readExcel(String excelpath) ...{
        Workbook wb = null;
        ArrayList list = new ArrayList();
        try ...{
            wb = Workbook.getWorkbook(new File(excelpath));
            Sheet sh = wb.getSheet(0);

            for (int j = 0; j < sh.getRows(); j++) ...{
                ArrayList list1 = new ArrayList();
                for (int i = 0; i < sh.getColumns(); i++) ...{
                    Cell cel = sh.getCell(i, j);
                    list1.add(cel.getContents());
                }
                list.add(list1);
            }

        } catch (BiffException e) ...{
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) ...{
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally ...{
            wb.close();
        }
        return list;

    }

    // public static void main(String[] args){
    // List list = RWExcel.readExcel("g:\aaaa.xls");
    // for(int i=0;i<list.size();i++){
    // System.out.println(list.get(i));
    // }
    // // TableTagResult tr = new TableTagResult("select * from employee");//这个是JDBC的结果集,Result返回List
    // // List list = tr.getTableResultRow();
    // // List titel = tr.getTableResultCol();
    // // RWExcel.writerExcel(list,titel, "g:\aaaa.xls");
    //
    // }

}

你可能感兴趣的:(java,jdbc,F#,J#)