Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook getSheetAt

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook getSheetAt

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFWorkbook getSheetAt.

Prototype

@Override
public HSSFSheet getSheetAt(int index) 

Source Link

Document

Get the HSSFSheet object at the given index.

Usage

From source file:com.yf.kp.design.siswa.FrameSiswa.java

public void fillData(File file) {
    try {/*from   www .  j av a 2  s .c o m*/
        siswaService = new SiswaServiceImpl();
        Siswa siswa;
        try (FileInputStream input = new FileInputStream(file)) {
            POIFSFileSystem fs = new POIFSFileSystem(input);
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0);
            Row row;
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                System.out.println(i);
                row = sheet.getRow(i);
                siswa = new Siswa();
                siswa.setNis(row.getCell(0).getStringCellValue());
                siswa.setNama(row.getCell(1).getStringCellValue());
                siswa.setKelas(row.getCell(2).getStringCellValue());
                siswa.setJenis_kelamin(row.getCell(3).getStringCellValue());
                siswa.setAgama(row.getCell(4).getStringCellValue());
                siswa.setTempat_lahir(row.getCell(5).getStringCellValue());
                siswa.setTgl_lahir(row.getCell(6).getDateCellValue());
                siswa.setAlamat(row.getCell(7).getStringCellValue());
                siswa.setNama_ortu(row.getCell(8).getStringCellValue());
                siswa.setPekerjaan(row.getCell(9).getStringCellValue());
                siswa.setAgama_ortu(row.getCell(10).getStringCellValue());
                siswa.setTelp(row.getCell(11).getStringCellValue());
                siswa.setAlamat_ortu(row.getCell(12).getStringCellValue());
                siswaService.save(siswa);
            }
        }
        JOptionPane.showMessageDialog(this, "Data Berhasil Dimasukan");
    } catch (HeadlessException | IOException | HibernateException | OfficeXmlFileException
            | IllegalStateException | NullPointerException e) {
        JOptionPane.showMessageDialog(this, "Error, Contact Your System Admin\n" + e.getMessage());
    }
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

private static List<List<Object>> readXLSWithHeader(InputStream inputStream) throws IOException {
    //        InputStream is = new FileInputStream(file);
    HSSFWorkbook wb = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = wb.getSheetAt(0);

    List<List<Object>> o = new ArrayList();
    List<Object> temp = null;
    if (sheet == null) {
        return null;
    }//www  .ja v  a 2 s .  c o m

    for (int row_index = 0; row_index <= sheet.getLastRowNum(); row_index++) {
        HSSFRow row = sheet.getRow(row_index);
        if (row == null) {
            continue;
        }
        temp = new ArrayList();
        for (int col_index = 0; col_index <= row.getLastCellNum(); col_index++) {
            temp.add(getCellValue(row.getCell(col_index)));
        }
        o.add(temp);
    }

    return o;
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

private static List<List<Object>> readXLS(InputStream inputStream) throws IOException {
    //        InputStream is = new FileInputStream(file);
    HSSFWorkbook wb = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = wb.getSheetAt(0);

    List<List<Object>> o = new ArrayList();
    List<Object> temp = null;
    if (sheet == null) {
        return null;
    }//from   w  ww . ja  v a2  s .c o  m

    for (int row_index = 1; row_index <= sheet.getLastRowNum(); row_index++) {
        HSSFRow row = sheet.getRow(row_index);
        if (row == null) {
            continue;
        }
        temp = new ArrayList();
        for (int col_index = 0; col_index <= row.getLastCellNum(); col_index++) {
            temp.add(getCellValue(row.getCell(col_index)));
        }
        o.add(temp);
    }

    return o;
}

From source file:com.yyl.common.utils.excel.ExcelTools.java

private static List<Map<String, String>> readXLS(InputStream inputStream, Map<String, Integer> keyMaps)
        throws IOException {
    //        InputStream is = new FileInputStream(file);
    HSSFWorkbook wb = new HSSFWorkbook(inputStream);

    List<Map<String, String>> list = new ArrayList();
    Map<String, String> temp = null;
    for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
        HSSFSheet sheet = wb.getSheetAt(sheetIndex);
        if (sheet == null) {
            continue;
        }/*from w w w  .  j  av  a2 s  . c  om*/
        for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
            HSSFRow row = sheet.getRow(rowIndex);
            temp = new HashMap();
            if (row != null) {
                for (Entry<String, Integer> entry : keyMaps.entrySet()) {
                    int index = entry.getValue();
                    HSSFCell cell = row.getCell(index);
                    temp.put(entry.getKey(), getCellValue(cell));
                }
            }
            list.add(temp);
        }
    }
    return list;
}

From source file:com.zdtx.ifms.specific.service.monitor.IpCamManager.java

@SuppressWarnings("unchecked")
public InputStream getExcel(String title) {
    List<Camera> data = new ArrayList<Camera>();
    Page<Camera> page_export = (Page<Camera>) Utils.getSession().getAttribute("page_export");
    DetachedCriteria criteria_export = (DetachedCriteria) Utils.getSession().getAttribute("criteria_export");
    List<Order> orderList_export = (List<Order>) Utils.getSession().getAttribute("orderList_export");
    page_export.setPageSize(page_export.getTotalCount());
    Page<Camera> pageResult = dao.getBatch(page_export, criteria_export.getExecutableCriteria(dao.getSession()),
            orderList_export);/*from  w  ww  . j a va  2  s. c om*/
    if (null != pageResult) {
        if (0 != pageResult.getResult().size()) {
            data = pageResult.getResult();

        }
    }
    ExportExcel ee = new ExportExcel() {

        @Override
        protected HSSFWorkbook disposeData(HSSFWorkbook wb, Object[] total, List<?> data) throws IOException {
            HSSFSheet sheet = wb.getSheetAt(0);
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
            HSSFRow rowss = sheet.createRow(0);
            rowss.setHeightInPoints(20);
            HSSFCell hssfCell = rowss.createCell(0);
            hssfCell = this.createCell(wb, hssfCell, total[0].toString());
            HSSFCellStyle style = this.createStyle(wb);
            HSSFRow row2 = sheet.createRow(1);
            HSSFCell cellrow01 = row2.createCell(0);
            cellrow01.setCellStyle(style);
            HSSFCell cellrow02 = row2.createCell(1);
            cellrow02.setCellStyle(style);
            HSSFCell cellrow03 = row2.createCell(2);
            cellrow03.setCellStyle(style);
            HSSFCell cellrow04 = row2.createCell(3);
            cellrow04.setCellStyle(style);
            HSSFCell cellrow05 = row2.createCell(4);
            cellrow05.setCellStyle(style);
            HSSFCell cellrow06 = row2.createCell(5);
            cellrow06.setCellStyle(style);
            HSSFCell cellrow07 = row2.createCell(6);
            cellrow07.setCellStyle(style);

            cellrow01.setCellValue("No.");
            cellrow02.setCellValue("Camera Name");
            cellrow03.setCellValue("Department");
            cellrow04.setCellValue("Camera Model");
            cellrow05.setCellValue("IP");
            cellrow06.setCellValue("Login Username");
            cellrow07.setCellValue("Login Password");

            if (null != data && 0 != data.size()) {

                for (int i = 0; i < data.size(); i++) {
                    Object[] o = (Object[]) data.get(i);
                    CamModel cm = (CamModel) o[0];
                    Camera a = (Camera) o[1];

                    HSSFRow row = sheet.createRow(i + 2);
                    HSSFCell cell001 = row.createCell(0);
                    cell001.setCellStyle(style);
                    cell001.setCellValue(i + 1);
                    HSSFCell cell002 = row.createCell(1);
                    cell002.setCellStyle(style);
                    cell002.setCellValue(a.getCameraName());
                    HSSFCell cell003 = row.createCell(2);
                    cell003.setCellStyle(style);
                    cell003.setCellValue(a.getDeptname());
                    HSSFCell cell004 = row.createCell(3);
                    cell004.setCellStyle(style);
                    cell004.setCellValue(cm.getModelName());

                    HSSFCell cell005 = row.createCell(4);
                    cell005.setCellStyle(style);
                    cell005.setCellValue(a.getIpAddress());

                    HSSFCell cell006 = row.createCell(5);
                    cell006.setCellStyle(style);
                    cell006.setCellValue(a.getAdminName());
                    HSSFCell cell007 = row.createCell(6);
                    cell007.setCellStyle(style);
                    cell007.setCellValue(a.getAdminPass());

                }
            }

            return wb;
        }
    };

    Object[] total = new Object[1];
    total[0] = title;
    String str = title;
    return ee.export(total, data, str);
}

From source file:com.zdtx.ifms.specific.service.task.FuelMileageManager.java

@SuppressWarnings("unchecked")
public InputStream getData(Page<Mileageoil> page, List<Mileageoil> list, FuelMileageVo fmvo) {

    //Page<Mileageoil> pageResult = this.getBetch(page, fmvo);

    Page<Mileageoil> page_export = (Page<Mileageoil>) Struts2Util.getSession().getAttribute("page_export");
    DetachedCriteria criteria_export = (DetachedCriteria) Struts2Util.getSession()
            .getAttribute("criteria_export");
    List<Order> orderList_export = (List<Order>) Struts2Util.getSession().getAttribute("orderList_export");
    page_export.setPageSize(page_export.getTotalCount());
    Page<Mileageoil> pageResult = baseDao.getBatch(page_export,
            criteria_export.getExecutableCriteria(baseDao.getSession()), orderList_export);

    List<Mileageoil> data = new ArrayList<Mileageoil>();

    if (null != pageResult) {
        if (0 != pageResult.getResult().size()) {
            data = pageResult.getResult();
        }// w  w w  . j a v a2  s . c  o  m
    }
    ExportExcel ee = new ExportExcel() {

        @Override
        protected HSSFWorkbook disposeData(HSSFWorkbook wb, Object[] total, List<?> data) throws IOException {
            HSSFSheet sheet = wb.getSheetAt(0);
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
            HSSFRow rowss = sheet.createRow(0);
            rowss.setHeightInPoints(20);
            HSSFCell hssfCell = rowss.createCell(0);
            hssfCell = this.createCell(wb, hssfCell, "Fuel Mileage");
            HSSFCellStyle style = this.createStyle(wb);
            HSSFRow row2 = sheet.createRow(1);
            HSSFCell cellrow01 = row2.createCell(0);
            cellrow01.setCellStyle(style);
            HSSFCell cellrow02 = row2.createCell(1);
            cellrow02.setCellStyle(style);
            HSSFCell cellrow03 = row2.createCell(2);
            cellrow03.setCellStyle(style);
            HSSFCell cellrow04 = row2.createCell(3);
            cellrow04.setCellStyle(style);
            HSSFCell cellrow05 = row2.createCell(4);
            cellrow05.setCellStyle(style);
            HSSFCell cellrow06 = row2.createCell(5);
            cellrow06.setCellStyle(style);
            cellrow01.setCellValue("No.");
            cellrow02.setCellValue("Plate Number");
            cellrow03.setCellValue("Vehicle Type");
            cellrow04.setCellValue("Date");
            cellrow05.setCellValue("Mileage");
            cellrow06.setCellValue("Fuel consumption");
            if (null != data && 0 != data.size()) {
                for (int i = 0; i < data.size(); i++) {
                    HSSFRow row = sheet.createRow(i + 2);
                    Mileageoil o = (Mileageoil) data.get(i);
                    HSSFCell cell001 = row.createCell(0);
                    cell001.setCellStyle(style);
                    cell001.setCellValue(i + 1);
                    HSSFCell cell002 = row.createCell(1);
                    cell002.setCellStyle(style);
                    cell002.setCellValue(o.getVehiclename());
                    HSSFCell cell003 = row.createCell(2);
                    cell003.setCellStyle(style);
                    cell003.setCellValue(o.getTypename());
                    HSSFCell cell004 = row.createCell(3);
                    cell004.setCellStyle(style);
                    cell004.setCellValue(o.getRiqi());
                    HSSFCell cell005 = row.createCell(4);
                    cell005.setCellStyle(style);
                    cell005.setCellValue(o.getMileage() + "m");
                    HSSFCell cell006 = row.createCell(5);
                    cell006.setCellStyle(style);
                    cell006.setCellValue(o.getOilcost() == null ? "0" : o.getOilcost() + "L");

                }
            }
            return wb;
        }

    };

    Object[] total = new Object[1];
    total[0] = "";
    String str = "Fuel Mileage";
    return ee.export(total, data, str);
}

From source file:com.zdtx.ifms.specific.service.vehicle.VehicleListManager.java

@SuppressWarnings("unchecked")
public InputStream getExcel(String title) {
    List<VehcileView> data = new ArrayList<VehcileView>();
    Page<VehcileView> page_export = (Page<VehcileView>) Utils.getSession().getAttribute("page_export");
    DetachedCriteria criteria_export = (DetachedCriteria) Utils.getSession().getAttribute("criteria_export");
    List<Order> orderList_export = (List<Order>) Utils.getSession().getAttribute("orderList_export");
    page_export.setPageSize(page_export.getTotalCount());
    Page<VehcileView> pageResult = baseDao.getBatch(page_export,
            criteria_export.getExecutableCriteria(baseDao.getSession()), orderList_export);
    if (null != pageResult) {
        if (0 != pageResult.getResult().size()) {
            data = pageResult.getResult();

        }/*from w  ww . j a v a  2 s  . c  om*/
    }
    ExportExcel ee = new ExportExcel() {

        @Override
        protected HSSFWorkbook disposeData(HSSFWorkbook wb, Object[] total, List<?> data) throws IOException {
            HSSFSheet sheet = wb.getSheetAt(0);
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
            HSSFRow rowss = sheet.createRow(0);
            rowss.setHeightInPoints(20);
            HSSFCell hssfCell = rowss.createCell(0);
            hssfCell = this.createCell(wb, hssfCell, total[0].toString());
            HSSFCellStyle style = this.createStyle(wb);
            HSSFRow row2 = sheet.createRow(1);
            HSSFCell cellrow01 = row2.createCell(0);
            cellrow01.setCellStyle(style);
            HSSFCell cellrow02 = row2.createCell(1);
            cellrow02.setCellStyle(style);
            HSSFCell cellrow03 = row2.createCell(2);
            cellrow03.setCellStyle(style);
            HSSFCell cellrow04 = row2.createCell(3);
            cellrow04.setCellStyle(style);
            HSSFCell cellrow05 = row2.createCell(4);
            cellrow05.setCellStyle(style);
            HSSFCell cellrow06 = row2.createCell(5);
            cellrow06.setCellStyle(style);
            HSSFCell cellrow07 = row2.createCell(6);
            cellrow07.setCellStyle(style);
            HSSFCell cellrow08 = row2.createCell(7);
            cellrow08.setCellStyle(style);
            HSSFCell cellrow09 = row2.createCell(8);
            cellrow09.setCellStyle(style);
            HSSFCell cellrow10 = row2.createCell(9);
            cellrow10.setCellStyle(style);

            cellrow01.setCellValue("No.");
            cellrow02.setCellValue("Plate Number");
            cellrow03.setCellValue("Fleet");
            cellrow04.setCellValue("Department");

            cellrow05.setCellValue("Device");
            cellrow06.setCellValue("Vehicle Type");
            cellrow07.setCellValue("Vehicle Brand");
            cellrow08.setCellValue("Key Code");
            cellrow09.setCellValue("IP");
            cellrow10.setCellValue("Description");

            if (null != data && 0 != data.size()) {

                for (int i = 0; i < data.size(); i++) {
                    VehcileView a = (VehcileView) data.get(i);

                    HSSFRow row = sheet.createRow(i + 2);
                    HSSFCell cell001 = row.createCell(0);
                    cell001.setCellStyle(style);
                    cell001.setCellValue(i + 1);
                    HSSFCell cell002 = row.createCell(1);
                    cell002.setCellStyle(style);
                    cell002.setCellValue(a.getVehiclename());
                    HSSFCell cell003 = row.createCell(2);
                    cell003.setCellStyle(style);
                    cell003.setCellValue(a.getFleetname());
                    HSSFCell cell004 = row.createCell(3);
                    cell004.setCellStyle(style);
                    cell004.setCellValue(a.getDeptname());

                    HSSFCell cell005 = row.createCell(4);
                    cell005.setCellStyle(style);
                    cell005.setCellValue(a.getDevicename());

                    HSSFCell cell006 = row.createCell(5);
                    cell006.setCellStyle(style);
                    cell006.setCellValue(a.getTypename());

                    HSSFCell cell007 = row.createCell(6);
                    cell007.setCellStyle(style);
                    cell007.setCellValue(a.getBrandname());

                    HSSFCell cell008 = row.createCell(7);
                    cell008.setCellStyle(style);
                    cell008.setCellValue(a.getKeycode());

                    HSSFCell cell009 = row.createCell(8);
                    cell009.setCellStyle(style);
                    cell009.setCellValue(a.getCctvip());

                    HSSFCell cell010 = row.createCell(9);
                    cell010.setCellStyle(style);
                    cell010.setCellValue(a.getDescription());

                }
            }

            return wb;
        }
    };

    Object[] total = new Object[1];
    total[0] = title;
    String str = title;
    return ee.export(total, data, str);
}

From source file:com.zhu.action.CarAction.java

public void exportpeople(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) {//from  w  w  w  . j  a  va2 s. c  o m
    CarForm carForm = (CarForm) form;

    // webbookExcel
    HSSFWorkbook wb = new HSSFWorkbook();
    // webbooksheet,Excelsheet
    HSSFSheet sheet = wb.createSheet("?");
    // sheet0,??poiExcel?short
    HSSFRow row = sheet.createRow((int) 0);
    // ? 
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ?
    HSSFCell cell = row.createCell(0);
    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell(1);
    cell.setCellValue("???");
    cell.setCellStyle(style);
    // ? ??
    OrderService orderService = new OrderService();
    List<Orderinfo> list = orderService.getOrderCarDetail(carForm.getId());
    System.out.println(list.size());
    for (int i = 0; i < list.size(); i++) {
        row = sheet.createRow(i + 1);
        Orderinfo order = (Orderinfo) list.get(i);
        // ?
        row.createCell(0).setCellValue(order.getN1());
        row.createCell(1).setCellValue(order.getI1());
        row = sheet.createRow(i + list.size() + 1);
        row.createCell(0).setCellValue(order.getN2());
        row.createCell(1).setCellValue(order.getI2());
        row = sheet.createRow(i + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN3());
        row.createCell(1).setCellValue(order.getI3());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN4());
        row.createCell(1).setCellValue(order.getI4());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN5());
        row.createCell(1).setCellValue(order.getI5());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN6());
        row.createCell(1).setCellValue(order.getI6());
        row = sheet.createRow(
                i + list.size() + list.size() + list.size() + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN7());
        row.createCell(1).setCellValue(order.getI7());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size()
                + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN8());
        row.createCell(1).setCellValue(order.getI8());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size()
                + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN9());
        row.createCell(1).setCellValue(order.getI9());
        row = sheet.createRow(i + list.size() + list.size() + list.size() + list.size() + list.size()
                + list.size() + list.size() + list.size() + list.size() + 1);
        row.createCell(0).setCellValue(order.getN10());
        row.createCell(1).setCellValue(order.getI10());
    }
    // ?
    String filePath = "";
    Date dt = new Date();
    DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    String date = df.format(dt).toString();
    filePath = "/Users/Nemo/Documents/carpeople" + date + ".xls";
    File file = new File(filePath);

    try {
        // FileOutputStream fout = new FileOutputStream(
        // "/Users/Nemo/Documents/carpeople.xls");
        // wb.write(fout);
        // fout.close();
        OutputStream out = new FileOutputStream(file);
        wb.write(out);
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    int key = 0;
    int MaxRowNum = 0, MaxCellNum = 0;
    try {
        FileInputStream in = new FileInputStream(filePath);
        POIFSFileSystem fs = new POIFSFileSystem(in);
        HSSFWorkbook workbook = new HSSFWorkbook(fs);
        FileOutputStream out = new FileOutputStream("/Users/Nemo/Documents/carpeople" + date + ".xls");
        int number = workbook.getNumberOfSheets();
        for (int i = 0; i < number; i++) { // ?sheet
            sheet = workbook.getSheetAt(i); // 14
            System.out.println("" + sheet.getSheetName() + " ? "
                    + (sheet.getLastRowNum() + 1));
            MaxRowNum = 0;
            for (int k = 0; k <= sheet.getLastRowNum(); k++) {
                HSSFRow hRow = sheet.getRow(k);
                // System.out.println((k + 1) + "");
                if (isBlankRow(hRow)) // 
                {
                    int m = 0;
                    for (m = k + 1; m <= sheet.getLastRowNum(); m++) {
                        HSSFRow nhRow = sheet.getRow(m);
                        if (!isBlankRow(nhRow)) {
                            // System.out.println("?" + (m + 1));
                            sheet.shiftRows(m, sheet.getLastRowNum(), k - m);
                            break;
                        }
                    }
                    if (m > sheet.getLastRowNum())
                        break; // ?
                } else { // ?
                    MaxRowNum++;
                    if (MaxCellNum < hRow.getLastCellNum())
                        MaxCellNum = hRow.getLastCellNum();
                }
            }
            workbook.setPrintArea(i, 0, MaxCellNum, 0, MaxRowNum);
            System.out
                    .println("?? " + sheet.getSheetName() + "  " + MaxRowNum);
        }
        workbook.write(out);
        in.close();
        out.close();
    } catch (IOException e) {
        System.out.println(key + " " + e.getMessage() + " ");
        e.printStackTrace();

    }

    System.out.println("??");

}

From source file:com.zxy.commons.poi.excel.ExcelUtils.java

License:Apache License

/**
 * ?Excelsheet// w  ww . j a  v  a  2 s  .  co  m
 * 
 * @param inputPath ???Excel
 * @return Excel?
 * @throws IOException IOException
 */
public static Map<String, Table<Integer, String, String>> readAll2table(String inputPath) throws IOException {
    Map<String, Table<Integer, String, String>> tables = Maps.newLinkedHashMap();
    FileInputStream inputStream = null;
    HSSFWorkbook wb = null;
    try {
        inputStream = new FileInputStream(inputPath);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        // HSSFWorkbook
        POIFSFileSystem fs = new POIFSFileSystem(bufferedInputStream);
        wb = new HSSFWorkbook(fs);
        List<String> columnNames = Lists.newLinkedList();
        for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
            Table<Integer, String, String> table = TreeBasedTable.create();
            HSSFSheet st = wb.getSheetAt(sheetIndex);
            String sheetName = st.getSheetName();
            for (int rowIndex = 0; rowIndex <= st.getLastRowNum(); rowIndex++) {
                HSSFRow row = st.getRow(rowIndex);
                for (int columnIndex = 0; columnIndex < row.getLastCellNum(); columnIndex++) {
                    HSSFCell cell = row.getCell(columnIndex);
                    if (cell != null) {
                        if (rowIndex == 0) { // 
                            columnNames.add(cell.getStringCellValue());
                        } else {
                            String value = cell.getStringCellValue();
                            table.put(rowIndex, columnNames.get(columnIndex), value);
                        }
                    }
                }
            }
            tables.put(sheetName, table);
        }
        return tables;
    } finally {
        if (wb != null) {
            wb.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }
}

From source file:controlador.controlador.java

@RequestMapping(value = "/cliente/abreExcel", method = RequestMethod.POST)
public ModelAndView abreExcel(ModelMap model, @RequestParam("archivo") File archivo,
        HttpServletRequest request) {//from   www .  j a  v  a  2s. c o m

    try {
        String var = "C:\\" + archivo.getPath();
        System.out.print(var);

        File objeto = new File(var);
        Workbook wb;
        //wb= WorkbookFactory.create(new FileInputStream(objeto));
        FileInputStream inp = new FileInputStream(var);
        HSSFWorkbook workbook = new HSSFWorkbook(inp);

        HSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        Row row;

        if (rowIterator.hasNext()) {
            row = rowIterator.next();
        }

        while (rowIterator.hasNext()) {
            row = rowIterator.next();
            Usuario u = new Usuario();
            Cliente cliente;
            Empleado empleado;

            String pass = row.getCell(9).getStringCellValue();
            String rol = row.getCell(10).getStringCellValue();
            String login_u = row.getCell(11).getStringCellValue();
            String password = null;
            MessageDigest md;
            try {
                md = MessageDigest.getInstance("MD5");
                md.update(pass.getBytes());
                byte[] digest = md.digest();
                StringBuilder sb = new StringBuilder();
                for (byte b : digest) {
                    sb.append(String.format("%02x", b & 0xff));
                }
                password = sb.toString();
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(controlador.class.getName()).log(Level.SEVERE, null, ex);
            }

            u.setLogin_usuario(login_u);
            u.setPassword_usuario(password);
            u.setRol_usuario(rol);
            usuario_bd.crearUsuario(u);

            Usuario usu = usuario_bd.daID(login_u);
            System.out.print(usu.getId_usuario() + "  " + login_u);
            cliente = new Cliente(usu, row.getCell(0).getStringCellValue(), row.getCell(1).getStringCellValue(),
                    row.getCell(2).getStringCellValue(), row.getCell(3).getStringCellValue(),
                    row.getCell(4).getStringCellValue(), row.getCell(5).getStringCellValue(),
                    row.getCell(6).getStringCellValue(), row.getCell(7).getStringCellValue(),
                    row.getCell(8).getStringCellValue(), 1);
            String url = "http://" + request.getServerName() + ":" + request.getServerPort()
                    + request.getContextPath();
            String token = "";
            mail_sender.send(construirResetTokenEmail(url, token, cliente.getCorreo(), null));

            cliente_bd.crearCliente(cliente);
            List clientes = cliente_bd.porCorreo(row.getCell(0).getStringCellValue());
            Cliente auxCli = (Cliente) clientes.get(0);

            System.out.print(row.getCell(13));
            int horas = (int) (row.getCell(13).getNumericCellValue());
            int hijos = (int) (row.getCell(16).getNumericCellValue());
            int sueldo = (int) (row.getCell(19).getNumericCellValue());
            int candidato = (int) (row.getCell(21).getNumericCellValue());
            Date fecha = row.getCell(17).getDateCellValue();

            empleado = new Empleado(auxCli.getId_cliente(), row.getCell(12).getStringCellValue(), horas,
                    row.getCell(14).getStringCellValue(), row.getCell(15).getStringCellValue(), hijos, fecha,
                    row.getCell(18).getStringCellValue(), String.valueOf(sueldo),
                    row.getCell(20).getStringCellValue(), String.valueOf(candidato), 1);

            empleado_bd.crearEmpleado(empleado);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    //workbook.close();
    model.addAttribute("clientes", cliente_bd.getClientes());
    return new ModelAndView("redirect:/home");
}