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
            }
        }
    

}

原文地址:https://www.cnblogs.com/mfrbuaa/p/3958261.html