Example usage for org.apache.poi.hssf.usermodel HSSFCell setCellValue

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell setCellValue

Introduction

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

Prototype

@SuppressWarnings("fallthrough")
public void setCellValue(boolean value) 

Source Link

Document

set a boolean value for the cell

Usage

From source file:com.krawler.esp.servlets.exportExcel.java

License:Open Source License

public void exportexcel(HttpServletResponse response, JSONObject jobj, java.util.Hashtable ht,
        String sheetTitle, String fileName, JSONArray hdr, JSONArray xlshdr, String heading, String[] xtypeArr,
        com.krawler.spring.exportFunctionality.exportDAOImpl exportDao) throws ServletException, IOException {
    try {//w  w  w .jav  a 2s  . co m
        response.setContentType("application/vnd.ms-excel");
        if (!StringUtil.isNullOrEmpty(heading)) {
            fileName = heading + fileName;
        }
        response.setHeader("Content-Disposition", "attachement; filename=" + fileName + ".xls");
        HSSFSheet sheet = wb.createSheet(sheetTitle);
        CellStyle cs = wb.createCellStyle();
        cs.setWrapText(true);

        HSSFHeader hh = sheet.getHeader();
        int j = 1;
        int width = 0;
        int maxrowno = 0;
        HSSFRow row1 = sheet.createRow((short) maxrowno);
        HashMap hm = extractData(jobj);
        JSONArray jarr = (JSONArray) hm.get("data");
        JSONObject tempObj;
        for (int k = 0; k < jarr.length(); k++) {
            tempObj = jarr.getJSONObject(k);
            HSSFRow row = sheet.createRow((short) j);
            int cellcount = 0;
            for (int i = 0; i < hdr.length(); i++) {
                Object str = tempObj.optString(hdr.getString(i), "");
                try {
                    if (xtypeArr.length > 0) {
                        str = convertValue(tempObj.optString(hdr.getString(i), ""), xtypeArr[i]);
                    }
                } catch (Exception e) {

                }
                if (ht.containsValue(hdr.getString(i))) {
                    if (j == maxrowno + 1) {
                        HSSFCell cell1 = row1.createCell(cellcount);
                        cell1.setCellStyle(cs);

                        width = xlshdr.getString(i).length() * 325;
                        if (width > sheet.getColumnWidth(cellcount)) {
                            sheet.setColumnWidth(cellcount, width);
                        }
                        HSSFFont font = wb.createFont();
                        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                        HSSFRichTextString hst = new HSSFRichTextString(xlshdr.getString(i));
                        hst.applyFont(font);
                        cell1.setCellValue(hst);

                    }
                    HSSFCell cell = row.createCell(cellcount);
                    cell.setCellStyle(cs);

                    if (str instanceof Date) {
                        cal.setTime((Date) str);
                        cell.setCellValue(cal);
                        cell.setCellStyle(this.dateCellStyle);
                        width = 4500;
                    } else if (str instanceof Number) {
                        cell.setCellValue(((Number) str).doubleValue());
                        width = 4500;
                    } else {
                        String colvalue = str.toString();
                        cell.setCellValue(new HSSFRichTextString(colvalue));
                        width = colvalue.length() * 325;
                    }

                    width = Math.min(width, MAX_CELL_WIDTH);

                    if (width > sheet.getColumnWidth(cellcount)) {
                        sheet.setColumnWidth(cellcount, width);
                    }

                    cellcount++;
                }
            }
            j++;

        }

        ConfigReader cr = ConfigReader.getinstance();
        String dirpath = cr.get("store");
        String path = dirpath + "baitheader.png";

        //                this.addimage(path,HSSFWorkbook.PICTURE_TYPE_PNG, wb, sheet,0,0,0,0,0,0,12,4);
        if (true) {
            OutputStream out = response.getOutputStream();
            wb.write(out);
            out.close();
        }
    } catch (JSONException ex) {
        Logger.getLogger(exportExcel.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(exportExcel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.krawler.esp.servlets.exportExcel.java

License:Open Source License

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("application/vnd.ms-excel");
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow(0);/*from  www  .ja  v a2 s  .com*/
    HSSFCell cell = row.createCell(0);
    cell.setCellValue(1);

    // Or do it on one line.
    row.createCell(1).setCellValue(1.2);

    row.createCell(3).setCellValue(true);
    // Write the output
    OutputStream out = response.getOutputStream();
    wb.write(out);
    out.close();
}

From source file:com.learn.core.utils.HSSFReadWrite.java

License:Apache License

/**
 * given a filename this outputs a sample sheet with just a set of
 * rows/cells./*ww  w.  j  a v a2 s  .  co  m*/
 */
private static void testCreateSampleSheet(String outputFilename) throws IOException {
    try (HSSFWorkbook wb = new HSSFWorkbook()) {
        HSSFSheet s = wb.createSheet();
        HSSFCellStyle cs = wb.createCellStyle();
        HSSFCellStyle cs2 = wb.createCellStyle();
        HSSFCellStyle cs3 = wb.createCellStyle();
        HSSFFont f = wb.createFont();
        HSSFFont f2 = wb.createFont();

        f.setFontHeightInPoints((short) 12);
        f.setColor((short) 0xA);
        f.setBold(true);
        f2.setFontHeightInPoints((short) 10);
        f2.setColor((short) 0xf);
        f2.setBold(true);
        cs.setFont(f);
        cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
        cs2.setBorderBottom(BorderStyle.THIN);
        cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cs2.setFillForegroundColor((short) 0xA);
        cs2.setFont(f2);
        wb.setSheetName(0, "HSSF Test");
        int rownum;
        for (rownum = 0; rownum < 300; rownum++) {
            HSSFRow r = s.createRow(rownum);
            if ((rownum % 2) == 0) {
                r.setHeight((short) 0x249);
            }

            for (int cellnum = 0; cellnum < 50; cellnum += 2) {
                HSSFCell c = r.createCell(cellnum);
                c.setCellValue(
                        rownum * 10000 + cellnum + (((double) rownum / 1000) + ((double) cellnum / 10000)));
                if ((rownum % 2) == 0) {
                    c.setCellStyle(cs);
                }
                c = r.createCell(cellnum + 1);
                c.setCellValue(new HSSFRichTextString("TEST"));
                // 50 characters divided by 1/20th of a point
                s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05));
                if ((rownum % 2) == 0) {
                    c.setCellStyle(cs2);
                }
            }
        }

        // draw a thick black border on the row at the bottom using BLANKS
        rownum++;
        rownum++;
        HSSFRow r = s.createRow(rownum);
        cs3.setBorderBottom(BorderStyle.THICK);
        for (int cellnum = 0; cellnum < 50; cellnum++) {
            HSSFCell c = r.createCell(cellnum);
            c.setCellStyle(cs3);
        }
        s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
        s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110));

        // end draw thick black border
        // create a sheet, set its title then delete it
        wb.createSheet();
        wb.setSheetName(1, "DeletedSheet");
        wb.removeSheetAt(1);

        // end deleted sheet
        try (FileOutputStream out = new FileOutputStream(outputFilename)) {
            wb.write(out);
        }
    }
}

From source file:com.learn.core.utils.HSSFReadWrite.java

License:Apache License

/**
  * Method main//from  www . jav  a2 s.  c o m
  *
  * Given 1 argument takes that as the filename, inputs it and dumps the
  * cell values/types out to sys.out.<br>
  *
  * given 2 arguments where the second argument is the word "write" and the
  * first is the filename - writes out a sample (test) spreadsheet
  * see {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br>
  *
  * given 2 arguments where the first is an input filename and the second
  * an output filename (not write), attempts to fully read in the
  * spreadsheet and fully write it out.<br>
  *
  * given 3 arguments where the first is an input filename and the second an
  * output filename (not write) and the third is "modify1", attempts to read in the
  * spreadsheet, deletes rows 0-24, 74-99.  Changes cell at row 39, col 3 to
  * "MODIFIED CELL" then writes it out.  Hence this is "modify test 1".  If you
  * take the output from the write test, you'll have a valid scenario.
  */
public static void main(String[] args) {
    if (args.length < 1) {
        System.err.println("At least one argument expected");
        return;
    }

    String fileName = args[0];
    try {
        if (args.length < 2) {

            try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) {
                System.out.println("Data dump:\n");

                for (int k = 0; k < wb.getNumberOfSheets(); k++) {
                    HSSFSheet sheet = wb.getSheetAt(k);
                    int rows = sheet.getPhysicalNumberOfRows();
                    System.out
                            .println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows + " row(s).");
                    for (int r = 0; r < rows; r++) {
                        HSSFRow row = sheet.getRow(r);
                        if (row == null) {
                            continue;
                        }

                        System.out.println("\nROW " + row.getRowNum() + " has " + row.getPhysicalNumberOfCells()
                                + " cell(s).");
                        for (int c = 0; c < row.getLastCellNum(); c++) {
                            HSSFCell cell = row.getCell(c);
                            String value;

                            if (cell != null) {
                                switch (cell.getCellTypeEnum()) {

                                case FORMULA:
                                    value = "FORMULA value=" + cell.getCellFormula();
                                    break;

                                case NUMERIC:
                                    value = "NUMERIC value=" + cell.getNumericCellValue();
                                    break;

                                case STRING:
                                    value = "STRING value=" + cell.getStringCellValue();
                                    break;

                                case BLANK:
                                    value = "<BLANK>";
                                    break;

                                case BOOLEAN:
                                    value = "BOOLEAN value-" + cell.getBooleanCellValue();
                                    break;

                                case ERROR:
                                    value = "ERROR value=" + cell.getErrorCellValue();
                                    break;

                                default:
                                    value = "UNKNOWN value of type " + cell.getCellTypeEnum();
                                }
                                System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value);
                            }
                        }
                    }
                }
            }
        } else if (args.length == 2) {
            if (args[1].toLowerCase(Locale.ROOT).equals("write")) {
                System.out.println("Write mode");
                long time = System.currentTimeMillis();
                HSSFReadWrite.testCreateSampleSheet(fileName);

                System.out.println("" + (System.currentTimeMillis() - time) + " ms generation time");
            } else {
                System.out.println("readwrite test");
                try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) {
                    try (FileOutputStream stream = new FileOutputStream(args[1])) {
                        wb.write(stream);
                    }
                }
            }
        } else if (args.length == 3 && args[2].equalsIgnoreCase("modify1")) {
            // delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"

            try (HSSFWorkbook wb = HSSFReadWrite.readFile(fileName)) {
                HSSFSheet sheet = wb.getSheetAt(0);

                for (int k = 0; k < 25; k++) {
                    HSSFRow row = sheet.getRow(k);

                    sheet.removeRow(row);
                }
                for (int k = 74; k < 100; k++) {
                    HSSFRow row = sheet.getRow(k);

                    sheet.removeRow(row);
                }
                HSSFRow row = sheet.getRow(39);
                HSSFCell cell = row.getCell(3);
                cell.setCellValue("MODIFIED CELL!!!!!");

                try (FileOutputStream stream = new FileOutputStream(args[1])) {
                    wb.write(stream);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.leosys.core.utils.ExcelUtil.java

public void exportExcel(List<?> dataList, OutputStream out) throws Exception {
    HSSFWorkbook workbook = null;//from  w  w w  . ja  v a2s  . c o  m
    HSSFSheet sheet = null;
    HSSFRow row = null;
    HSSFCell cell = null;
    HSSFCellStyle titleStyle = null;
    int rowIndex = 0;
    try {
        workbook = new HSSFWorkbook();// 
        sheet = workbook.createSheet("?");// ?      
        sheet.setDefaultColumnWidth((short) 30);// 15   
        titleStyle = workbook.createCellStyle();//?
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        titleStyle.setFillForegroundColor(HSSFColor.WHITE.index);
        titleStyle.setFillBackgroundColor(HSSFColor.WHITE.index);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFFont font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setFontHeightInPoints((short) 17);
        titleStyle.setFont(font);

        row = sheet.createRow(rowIndex++);
        row.setHeight((short) 600);
        for (short i = 0; i < headArr.length; i++) {
            cell = row.createCell(i); //?
            if (i == 0)
                cell.setCellValue(new HSSFRichTextString(title));
            cell.setCellStyle(titleStyle);
        }
        // ???    
        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) (headArr.length - 1)));

        titleStyle = workbook.createCellStyle();
        titleStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        font.setFontHeightInPoints((short) 13);
        titleStyle.setFont(font);// ??   
        row = sheet.createRow(rowIndex++);// 
        for (short i = 0; i < headArr.length; i++) {
            cell = row.createCell(i);
            cell.setCellStyle(titleStyle);
            cell.setCellValue(new HSSFRichTextString(headArr[i]));
        }
        //?
        titleStyle = workbook.createCellStyle();
        titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        if (dataList == null || dataList.isEmpty())
            return;
        short dataType = 0;//
        if (dataList.get(0) instanceof Map<?, ?>)
            dataType = 1;
        else if (dataList.get(0) instanceof List<?>)
            dataType = 2;
        if (dataType == 0) {
            String[] dataArr = null;
            for (Object data : dataList) {
                dataArr = (String[]) data;
                if (dataArr == null)
                    continue;
                row = sheet.createRow(rowIndex++);
                for (short i = 0; i < headArr.length; i++) {
                    if (i < dataArr.length) {
                        Object val = dataArr[i];
                        if (rendererArr != null && rendererArr[i] != null)
                            val = rendererArr[i].renderer(dataArr[i], i, dataArr);
                        fillCell(row, titleStyle, font, i, val);
                    }
                }
            }
        } else if (dataType == 1) {
            Map<?, ?> map = null;
            for (Object data : dataList) {
                map = (Map<?, ?>) data;
                if (map == null)
                    continue;
                Object[] dataArr = map.values().toArray();
                if (dataArr == null)
                    continue;
                row = sheet.createRow(rowIndex++);
                for (short i = 0; i < headArr.length; i++) {
                    if (i < dataArr.length) {
                        Object val = dataArr[i];
                        if (rendererArr != null && rendererArr[i] != null)
                            val = rendererArr[i].renderer(dataArr[i], i, dataArr);
                        fillCell(row, titleStyle, font, i, val);
                    }
                }
            }
        } else if (dataType == 2) {
            List<?> list = null;
            for (Object data : dataList) {
                list = (List<?>) data;
                if (list == null || list.isEmpty())
                    continue;
                row = sheet.createRow(rowIndex++);
                for (short i = 0; i < headArr.length; i++) {
                    if (i < list.size()) {
                        Object val = list.get(i);
                        if (rendererArr != null && rendererArr[i] != null)
                            val = rendererArr[i].renderer(list.get(i), i, list);
                        fillCell(row, titleStyle, font, i, val);
                    }
                }
            }
        } else
            throw new Exception("excel???");
        workbook.write(out);
    } catch (Exception e) {
        throw new Exception("excel" + e.getMessage());
    } finally {
        //         if(out != null){
        //            try {
        //               out.close();
        //            } catch (IOException e) {}
        //         }
    }
}

From source file:com.leosys.core.utils.ExcelUtil.java

private void fillCell(HSSFRow row, HSSFCellStyle style, HSSFFont font, Short cellIndex, Object value) {
    if (value == null)
        value = "";
    //         System.out.println(value.getClass().getName());
    HSSFCell cell = row.createCell(cellIndex);
    cell.setCellStyle(style);/*from  www  . j  a va 2s.  co m*/
    //      cell.
    HSSFRichTextString richStr = null;

    if (value instanceof Timestamp) {
        richStr = new HSSFRichTextString(this.toDateEnFull(((Timestamp) value)));
    } else
        richStr = new HSSFRichTextString(value.toString());

    richStr.applyFont(font);
    if (richStr != null)
        cell.setCellValue(richStr);
}

From source file:com.lingxiang2014.ExcelView.java

License:Open Source License

public void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Assert.notEmpty(properties);//from ww w.j a va 2s  .c  o m
    HSSFSheet sheet;
    if (StringUtils.isNotEmpty(sheetName)) {
        sheet = workbook.createSheet(sheetName);
    } else {
        sheet = workbook.createSheet();
    }
    int rowNumber = 0;
    if (titles != null && titles.length > 0) {
        HSSFRow header = sheet.createRow(rowNumber);
        header.setHeight((short) 400);
        for (int i = 0; i < properties.length; i++) {
            HSSFCell cell = header.createCell(i);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 11);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            if (i == 0) {
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                HSSFComment comment = patriarch
                        .createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 4, 4));
                comment.setString(new HSSFRichTextString("P" + "o" + "w" + "e" + "r" + "e" + "d" + " " + "B"
                        + "y" + " " + "S" + "H" + "O" + "P" + "+" + "+"));
                cell.setCellComment(comment);
            }
            if (titles.length > i && titles[i] != null) {
                cell.setCellValue(titles[i]);
            } else {
                cell.setCellValue(properties[i]);
            }
            if (widths != null && widths.length > i && widths[i] != null) {
                sheet.setColumnWidth(i, widths[i]);
            } else {
                sheet.autoSizeColumn(i);
            }
        }
        rowNumber++;
    }
    if (data != null) {
        for (Object item : data) {
            HSSFRow row = sheet.createRow(rowNumber);
            for (int i = 0; i < properties.length; i++) {
                HSSFCell cell = row.createCell(i);
                if (converters != null && converters.length > i && converters[i] != null) {
                    Class<?> clazz = PropertyUtils.getPropertyType(item, properties[i]);
                    ConvertUtils.register(converters[i], clazz);
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                    ConvertUtils.deregister(clazz);
                    if (clazz.equals(Date.class)) {
                        DateConverter dateConverter = new DateConverter();
                        dateConverter.setPattern(DEFAULT_DATE_PATTERN);
                        ConvertUtils.register(dateConverter, Date.class);
                    }
                } else {
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                }
                if (rowNumber == 0 || rowNumber == 1) {
                    if (widths != null && widths.length > i && widths[i] != null) {
                        sheet.setColumnWidth(i, widths[i]);
                    } else {
                        sheet.autoSizeColumn(i);
                    }
                }
            }
            rowNumber++;
        }
    }
    if (contents != null && contents.length > 0) {
        rowNumber++;
        for (String content : contents) {
            HSSFRow row = sheet.createRow(rowNumber);
            HSSFCell cell = row.createCell(0);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setColor(HSSFColor.GREY_50_PERCENT.index);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(content);
            rowNumber++;
        }
    }
    response.setContentType("application/force-download");
    if (StringUtils.isNotEmpty(filename)) {
        response.setHeader("Content-disposition",
                "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
    } else {
        response.setHeader("Content-disposition", "attachment");
    }
}

From source file:com.lition.service.impl.OwnedServiceImpl.java

/**
 * POI??/*from ww  w . j av a 2 s.c om*/
 */
@Override
public InputStream getOutExcelDate() {
    //1.?
    String headTitle[] = { "id", "?", "??", "", "??" };

    //2.Dao??
    List<OwnedVehicle> list = dao.queryAll();

    //3.?HSSFWorkbook
    HSSFWorkbook wb = new HSSFWorkbook();

    //4.?sheet
    HSSFSheet sheet = wb.createSheet("?");

    //5.?
    HSSFCellStyle style = wb.createCellStyle();
    // ??
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);

    //6.
    HSSFRow row0 = sheet.createRow(0);
    //7.??
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));

    HSSFCell row0cell0 = row0.createCell(0);
    row0cell0.setCellValue("?");
    row0cell0.setCellStyle(style);

    HSSFRow row1 = sheet.createRow(1);

    for (int i = 0; i < headTitle.length; i++) {
        HSSFCell row0cell = row1.createCell(i);
        row0cell.setCellValue(headTitle[i]);
        row0cell.setCellStyle(style);
    }

    for (int i = 0; i < list.size(); i++) {
        HSSFRow row = sheet.createRow(i + 2);

        //ID
        HSSFCell cell0 = row.createCell(0);
        cell0.setCellValue(list.get(i).getId());
        cell0.setCellStyle(style);

        //?
        HSSFCell cell1 = row.createCell(1);
        cell1.setCellValue(list.get(i).getVehicleId());
        cell1.setCellStyle(style);

        //??
        HSSFCell cell2 = row.createCell(2);
        cell2.setCellValue(list.get(i).getDepid());
        cell2.setCellStyle(style);

        //
        HSSFCell cell3 = row.createCell(3);
        cell3.setCellValue(list.get(i).getModel());
        cell3.setCellStyle(style);

        //??
        HSSFCell cell4 = row.createCell(4);
        cell4.setCellValue(list.get(i).getVehicleUsageId());
        cell4.setCellStyle(style);
    }

    //?inputstream;
    try {
        OutputStream out = new FileOutputStream("abc.xls");
        wb.write(out);
        out.close();
        InputStream in = new FileInputStream("abc.xls");
        return in;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

From source file:com.lushapp.common.excel.ExcelUtil.java

License:Apache License

/**
 * @param excel_name//from w  w w.  j  a va 2s  . co  m
 *            ?Excel+??
 * @param headList
 *            ExcelHead?
 * @param fieldList
 *            ExcelField?
 * @param dataList
 *            Excel?
 * @throws Exception
 */
public static void createExcel(String excel_name, String[] headList, String[] fieldList,
        List<Map<String, Object>> dataList) throws Exception {
    // Excel 
    HSSFWorkbook workbook = new HSSFWorkbook();

    // Excel???
    // ???""?
    // HSSFSheet sheet = workbook.createSheet("");
    HSSFSheet sheet = workbook.createSheet();
    // 0?
    HSSFRow row = sheet.createRow(0);
    // ===============================================================
    for (int i = 0; i < headList.length; i++) {

        // 0??
        HSSFCell cell = row.createCell(i);
        // ?
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        // ?
        cell.setCellValue(headList[i]);
    }
    // ===============================================================

    for (int n = 0; n < dataList.size(); n++) {
        // 1?
        HSSFRow row_value = sheet.createRow(n + 1);
        Map<String, Object> dataMap = dataList.get(n);
        // ===============================================================
        for (int i = 0; i < fieldList.length; i++) {

            // 0??
            HSSFCell cell = row_value.createCell(i);
            // ?
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            // ?
            cell.setCellValue(objToString(dataMap.get(fieldList[i])));
        }
        // ===============================================================
    }

    // ?
    FileOutputStream fOut = new FileOutputStream(excel_name);
    // Excel 
    workbook.write(fOut);
    fOut.flush();
    // ??
    fOut.close();
    //System.out.println("[" + excel_name + "]" + "?...");
}

From source file:com.lushapp.common.excel.ExcelUtil.java

License:Apache License

/**
 * @param excel_name/*from  ww w . j a va  2  s.  c  om*/
 *            ?Excel+??
 * @param headList
 *            ExcelHead?
 * @param fieldList
 *            ExcelField?
 * @param dataList
 *            Excel?
 * @throws Exception
 */
public static void createExcel(String excel_name, List<String> headList, List<String> fieldList,
        List<Map<String, Object>> dataList) throws Exception {
    // Excel 
    HSSFWorkbook workbook = new HSSFWorkbook();

    // Excel???
    // ???""?
    // HSSFSheet sheet = workbook.createSheet("");
    HSSFSheet sheet = workbook.createSheet();
    // 0?
    HSSFRow row = sheet.createRow(0);
    // ===============================================================
    for (int i = 0; i < headList.size(); i++) {

        // 0??
        HSSFCell cell = row.createCell(i);
        // ?
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        // ?
        cell.setCellValue(headList.get(i));
    }
    // ===============================================================

    for (int n = 0; n < dataList.size(); n++) {
        // 1?
        HSSFRow row_value = sheet.createRow(n + 1);
        Map<String, Object> dataMap = dataList.get(n);
        // ===============================================================
        for (int i = 0; i < fieldList.size(); i++) {

            // 0??
            HSSFCell cell = row_value.createCell(i);
            // ?
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            // ?
            cell.setCellValue(objToString(dataMap.get(fieldList.get(i))));
        }
        // ===============================================================
    }

    // ?
    FileOutputStream fOut = new FileOutputStream(excel_name);
    // Excel 
    workbook.write(fOut);
    fOut.flush();
    // ??
    fOut.close();
    //System.out.println("[" + excel_name + "]" + "?...");
}