zoukankan      html  css  js  c++  java
  • java导入excel

    package com.duosen.gate.test;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;

    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 ExcelReader {

        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ExcelReader.readExcel(new File("D:/a.xls"));
            ExcelReader.getExcel();
        }


            //private static ExcelInterface excelInterface = null;
            private static Workbook wb = null;
            //读取excel中的某一个 sheet.
            public static void readExcel(File excelFile){
                try {
                    FileInputStream is = new FileInputStream(excelFile);
                    //读取excel数据表的第一步是创建workbook工作簿
                    wb = Workbook.getWorkbook(is);
                    //通过 workbook来 訪问excel sheet
                    Sheet sh1 = wb.getSheet(0);
                    //通过 sheet訪问excel cell,获取第一行第一列 的值
                    // Cell c1 = sh1.getCell(1, 0);
                    //获得excel全部sheet的名称

                    /*String sheetNames[] = wb.getSheetNames();
        //读取一个excel里的全部sheet名称
           for (int i = 0; i < sheetNames.length; i++) {
            System.out.println("sheet name="+ i +sheetNames[i]);
           }*/
                    int columns = sh1.getColumns();

                    for (int i = 0; i < sh1.getRows(); i++) {
                        //String[] nextLine = new String[columns];
                        System.out.println();
                        for (int j = 0; j < columns; j++) {
                            //注意不论什么一个cell(单元格)getContents()以后都会得到一个字符串(无论它原来是什么类型,eg:整形、浮点型、字符....)
                            String cellVal = sh1.getCell(j, i).getContents();
                            //System.out.print(sh1.getCell(j, i).getContents()+" || ");
                            System.out.println(cellVal+" || ");
                        }
                    }
                    //关闭
                    wb.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (BiffException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            
            public static void getExcel(){
                try {
                    File f = new File("d:/a.xls");
                    WritableWorkbook workbook = Workbook.createWorkbook(f);
                    WritableSheet sheet = workbook.createSheet("1", 0);
                    for (int i = 0; i < 10; i++) {
                        for (int j = 0; j < 1000; j++) {
                            Label label = new Label(i,j,"sdss"+i+"-"+j);
                            sheet.addCell(label);
                        }
                    }
                    
                    workbook.write();
                    workbook.close();
                    
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        

    }

  • 相关阅读:
    textarea回车在多浏览器兼容问题
    windows server平台移动oracle表空间
    奇妙的英文recreate,reproduce,regenerate也不同
    《Inside the C++ Object Model》笔记(1~7章)
    1.4买书问题C#源码
    C#的Compiler Error CS1660
    数学符号表
    C#工程添加了DLL编译运行时却提示”无法加载DLL“的解决方案
    看源代码那些事【转】
    救命的软件,查看你关掉的网站内容
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3958261.html
Copyright © 2011-2022 走看看