Example usage for org.apache.poi.xssf.usermodel XSSFRow createCell

List of usage examples for org.apache.poi.xssf.usermodel XSSFRow createCell

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFRow createCell.

Prototype

@Override
public XSSFCell createCell(int columnIndex) 

Source Link

Document

Use this to create new cells within the row and return it.

Usage

From source file:in.expertsoftware.colorcheck.FormatvarificationErrorList.java

private static void CreaateStyleOfErrorList(XSSFWorkbook ErrorWorkbook, XSSFRow row, String cell_ref,
        String sheet_name, String error_desc, String error_level) {
    XSSFCellStyle StyleOfCell = ErrorWorkbook.createCellStyle();
    StyleOfCell.setAlignment(XSSFCellStyle.ALIGN_CENTER);
    StyleOfCell.setFillPattern(FillPatternType.SOLID_FOREGROUND);

    if (error_level.equalsIgnoreCase("Warning")) {
        StyleOfCell.setFillForegroundColor(new XSSFColor(new java.awt.Color(155, 194, 230)));
    } else {/*  w ww . j  a  v a 2s  . com*/
        StyleOfCell.setFillForegroundColor(new XSSFColor(new java.awt.Color(225, 171, 171)));
    }
    StyleOfCell.setBorderLeft((short) 1);
    StyleOfCell.setBorderRight((short) 1);
    StyleOfCell.setBorderTop((short) 1);
    StyleOfCell.setBorderBottom((short) 1);
    StyleOfCell.setWrapText(true);

    //create font
    XSSFFont fontOfCell = ErrorWorkbook.createFont();
    fontOfCell.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
    fontOfCell.setFontHeightInPoints((short) 10);
    fontOfCell.setFontName("Calibri");
    fontOfCell.setColor(new XSSFColor(new java.awt.Color(0, 0, 0)));
    StyleOfCell.setFont(fontOfCell);
    Cell Rowcell_0 = row.createCell(0);
    Cell Rowcell_1 = row.createCell(1);
    Cell Rowcell_2 = row.createCell(2);
    Cell Rowcell_3 = row.createCell(3);
    Rowcell_0.setCellValue(cell_ref);
    Rowcell_1.setCellValue(sheet_name);
    Rowcell_2.setCellValue(error_desc);
    Rowcell_3.setCellValue(error_level);
    Rowcell_0.setCellStyle(StyleOfCell);
    Rowcell_1.setCellStyle(StyleOfCell);
    Rowcell_2.setCellStyle(StyleOfCell);
    Rowcell_3.setCellStyle(StyleOfCell);
}

From source file:in.expertsoftware.colorcheck.FormatvarificationErrorList.java

private static void setColorInfoMetaData(XSSFWorkbook ErrorWorkbook) {
    //Set Colour used information on first sheet.
    XSSFSheet setInfoSheet = ErrorWorkbook.getSheetAt(0);
    XSSFRow colourInfoRow;
    XSSFRow errorColourRow;//from ww w.j ava2  s  .c o  m
    XSSFRow warningColourRow;
    if (setInfoSheet.getPhysicalNumberOfRows() > 5) {
        colourInfoRow = setInfoSheet.getRow(5);
    } else {
        colourInfoRow = setInfoSheet.createRow(5);
    }
    Cell colorInfoCell = colourInfoRow.createCell(6);
    Cell RGBCell = colourInfoRow.createCell(7);
    CreaateHeaderOfErrorList(ErrorWorkbook, colorInfoCell, "Used Color");
    CreaateHeaderOfErrorList(ErrorWorkbook, RGBCell, "RGB Value");
    setInfoSheet.autoSizeColumn(6);
    setInfoSheet.autoSizeColumn(7);
    if (setInfoSheet.getPhysicalNumberOfRows() > 6) {
        errorColourRow = setInfoSheet.getRow(6);
    } else {
        errorColourRow = setInfoSheet.createRow(6);
    }
    if (setInfoSheet.getPhysicalNumberOfRows() > 7) {
        warningColourRow = setInfoSheet.getRow(7);
    } else {
        warningColourRow = setInfoSheet.createRow(7);
    }
    //error color style
    XSSFCellStyle errorStyle = ErrorWorkbook.createCellStyle();
    errorStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    errorStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(225, 171, 171)));
    errorColourRow.createCell(6).setCellStyle(errorStyle);
    errorColourRow.getCell(6).setCellValue("Error");
    errorColourRow.createCell(7).setCellValue("225, 171, 171");
    //warning color style
    XSSFCellStyle warningStyle = ErrorWorkbook.createCellStyle();
    warningStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    warningStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(155, 194, 230)));
    warningColourRow.createCell(6).setCellStyle(warningStyle);
    warningColourRow.getCell(6).setCellValue("Warning");
    warningColourRow.createCell(7).setCellValue("155, 194, 230");
}

From source file:Interface.interemploi.java

private void writetoxel() {

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();//from w  w  w  .  j  a v a  2 s.  c  om

    TreeMap<String, Object[]> data = new TreeMap<>();
    data.put("0",
            new Object[] { mod.getColumnName(0), mod.getColumnName(1), mod.getColumnName(2),
                    mod.getColumnName(3), mod.getColumnName(4), mod.getColumnName(5), mod.getColumnName(6),
                    mod.getColumnName(7), mod.getColumnName(8), mod.getColumnName(9), mod.getColumnName(10),
                    mod.getColumnName(11), mod.getColumnName(12), mod.getColumnName(13), mod.getColumnName(14),
                    mod.getColumnName(15), mod.getColumnName(16), mod.getColumnName(17), mod.getColumnName(18),
                    mod.getColumnName(19) });
    int nb = mod.getRowCount();
    int s = 0;
    for (int i = 1; i <= nb; i++) {
        data.put(Integer.toString(i),
                new Object[] { getvlue(s, 0), getvlue(s, 1), getvlue(s, 2), getvlue(s, 3), getvlue(s, 4),
                        getvlue(s, 5), getvlue(s, 6), getvlue(s, 7), getvlue(s, 8), getvlue(s, 9),
                        getvlue(s, 10), getvlue(s, 11), getvlue(s, 12), getvlue(s, 13), getvlue(s, 14),
                        getvlue(s, 15), getvlue(s, 16), getvlue(s, 17), getvlue(s, 18), getvlue(s, 19) });

        s++;
    }
    Set<String> ids = data.keySet();
    XSSFRow row;
    int rowID = 0;
    for (String key : ids) {
        row = ws.createRow(rowID++);

        Object[] values = data.get(key);
        int cellID = 0;
        for (Object o : values) {
            Cell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }

    try {
        FileOutputStream fs;

        fs = new FileOutputStream(new File("bdd_Fonctionaire.xlsx"));
        wb.write(fs);
        fs.close();
        JOptionPane.showConfirmDialog(null, "Votre Base De Donnee Est Bien Expoter", "Valider",
                JOptionPane.CLOSED_OPTION);
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.OPEN)) {
            desktop.open(new File("bdd_Fonctionaire.xlsx"));
        } else {
            System.out.println("Open is not supported");
        }
    } catch (FileNotFoundException ex) {
        System.out.println("eruer fichier");
    } catch (IOException ex) {
        System.out.println("eruer fichier");
    }

}

From source file:Interface.interProf.java

private void writetoxel() {

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet();/*from  w  w  w.  ja  v  a 2 s  . c o m*/

    TreeMap<String, Object[]> data = new TreeMap<>();
    data.put("0",
            new Object[] { mod.getColumnName(0), mod.getColumnName(1), mod.getColumnName(2),
                    mod.getColumnName(3), mod.getColumnName(4), mod.getColumnName(5), mod.getColumnName(6),
                    mod.getColumnName(7), mod.getColumnName(8), mod.getColumnName(9), mod.getColumnName(10),
                    mod.getColumnName(11), mod.getColumnName(12), mod.getColumnName(13), mod.getColumnName(14),
                    mod.getColumnName(15), mod.getColumnName(16), mod.getColumnName(17), mod.getColumnName(18),
                    mod.getColumnName(19), mod.getColumnName(20) });
    int nb = mod.getRowCount();
    int s = 0;
    for (int i = 1; i <= nb; i++) {
        data.put(Integer.toString(i), new Object[] { getvlue(s, 0), getvlue(s, 1), getvlue(s, 2), getvlue(s, 3),
                getvlue(s, 4), getvlue(s, 5), getvlue(s, 6), getvlue(s, 7), getvlue(s, 8), getvlue(s, 9),
                getvlue(s, 10), getvlue(s, 11), getvlue(s, 12), getvlue(s, 13), getvlue(s, 14), getvlue(s, 15),
                getvlue(s, 16), getvlue(s, 17), getvlue(s, 18), getvlue(s, 19), getvlue(s, 20) });

        s++;
    }
    Set<String> ids = data.keySet();
    XSSFRow row;
    int rowID = 0;
    for (String key : ids) {
        row = ws.createRow(rowID++);

        Object[] values = data.get(key);
        int cellID = 0;
        for (Object o : values) {
            Cell cell = row.createCell(cellID++);
            cell.setCellValue(o.toString());
        }
    }

    try {
        FileOutputStream fs;

        fs = new FileOutputStream(new File("bdd_prof.xlsx"));
        wb.write(fs);
        fs.close();
        JOptionPane.showConfirmDialog(null, "Votre Base De Donnee Est Bien Expoter", "Valider",
                JOptionPane.CLOSED_OPTION);

        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.OPEN)) {
            desktop.open(new File("bdd_prof.xlsx"));
        } else {
            System.out.println("Open is not supported");
        }

    } catch (FileNotFoundException ex) {
        System.out.println("eruer fichier");
    } catch (IOException ex) {
        System.out.println("eruer fichier");
    }

}

From source file:io.excel.AnnotationExcelIO.java

public void save() {

    resultEditor.annotations.Depot depot = new resultEditor.annotations.Depot();

    try {//from   w w w . j a  va 2  s.  com

        int rowcount = 0;
        File project = env.Parameters.WorkSpace.CurrentProject;

        if (project == null) {
            return;
        }

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet1 = wb.createSheet("Annotation_by_att");

        //
        XSSFRow row0 = sheet1.createRow(0);
        row0.createCell((short) 0).setCellValue("Project Name");
        row0.createCell((short) 1).setCellValue("Annotation ID");
        row0.createCell((short) 2).setCellValue("Annotation Text");
        row0.createCell((short) 3).setCellValue("Annotation Class");
        row0.createCell((short) 4).setCellValue("Full Span");
        row0.createCell((short) 5).setCellValue("Span Start");
        row0.createCell((short) 6).setCellValue("Span End");
        row0.createCell((short) 7).setCellValue("Created Date");
        row0.createCell((short) 8).setCellValue("Annotator Name");
        row0.createCell((short) 9).setCellValue("Annotator ID");
        row0.createCell((short) 10).setCellValue("Attributes");
        row0.createCell((short) 11).setCellValue("Relaionships");
        row0.createCell((short) 11).setCellValue("Contents");

        for (Article article : depot.getDepot()) {
            if (article == null) {
                continue;
            }
            if (article.annotations == null) {
                continue;
            }

            // go through all annotation for each article
            for (Annotation annotation : article.annotations) {
                if (annotation == null) {
                    continue;
                }

                rowcount++;
                XSSFRow row1 = sheet1.createRow(rowcount);
                // project name
                row1.createCell((short) 0).setCellValue(project.getName());
                // annotation id
                row1.createCell((short) 1).setCellValue(annotation.mentionid);
                // current annotator name                   
                row1.createCell((short) 2).setCellValue(annotation.annotationText);
                // class
                row1.createCell((short) 3).setCellValue(annotation.annotationclass);
                // span info
                row1.createCell((short) 4).setCellValue(annotation.getSpansInText());
                row1.createCell((short) 5).setCellValue(annotation.spanset.getMinimumStart());
                row1.createCell((short) 6).setCellValue(annotation.spanset.getMaximumEnd());
                // create time
                row1.createCell((short) 7).setCellValue(annotation.creationDate);
                // annotator
                row1.createCell((short) 8).setCellValue(annotation.getAnnotator());
                row1.createCell((short) 9).setCellValue(annotation.annotatorid);
                row1.createCell((short) 10).setCellValue(annotation.getAttributeString());
                row1.createCell((short) 11).setCellValue(annotation.getComplexRelationshipString());

                String textcontent = PreLoadDocumentContents.getSurroundText(annotation, article);
                row1.createCell((short) 12).setCellValue(textcontent);
            }

        }

        // BUILD THE FILE NAME
        String filename = project.getAbsolutePath() + File.separatorChar + "AnnotationsInExcel.xlsx";
        System.out.println("Annotation results are saved on MS EXCEL Format:" + filename);

        // OUTPUT THE FILE TO DISK
        FileOutputStream fileOut = new FileOutputStream(filename);
        wb.write(fileOut);
        fileOut.close(); // CLOSE

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:jpgtoxlsx.JPGtoXLSX.java

/**
 * @param args the command line arguments
 *///from ww  w  .  j  a v  a  2 s  .c  o m
public static void main(String[] args) throws Exception {

    XSSFWorkbook myExcel = new XSSFWorkbook();
    XSSFSheet sheet = myExcel.createSheet("Image");

    BufferedImage image = null;

    int width, height;
    int xlrows;
    //int pixel;

    File fimg;
    //open image
    try {
        fimg = new File("C:\\excel\\Test.jpg");
        image = ImageIO.read(fimg);
    } catch (IOException e) {
        System.out.println(e);
    }

    width = image.getWidth();
    //System.out.println(width);
    height = image.getHeight();
    //System.out.println(height);

    xlrows = height * 3;

    //System.out.println(pixel);

    int r, g, b;

    int w = image.getWidth();
    int h = image.getHeight();
    System.out.println("Width: " + w + "Height: " + h);
    System.out.println("Generating RGB values..");

    XSSFCellStyle style1 = myExcel.createCellStyle();
    XSSFCellStyle style2 = myExcel.createCellStyle();
    XSSFCellStyle style3 = myExcel.createCellStyle();

    for (int i = 1; i < ++h; i++) {

        if (i == image.getHeight()) {
            break;
        }

        XSSFRow row1 = sheet.createRow((i * 3 - 3));
        XSSFRow row2 = sheet.createRow((i * 3 - 2));
        XSSFRow row3 = sheet.createRow((i * 3 - 1));

        for (int j = 0; j < ++w; j++) {

            if (j == image.getWidth()) {
                break;
            }

            //System.out.println("I: " + i);
            //System.out.println("J: " + j + "\n");
            int x = i;
            int y = j;

            //System.out.println("X: " + x + "Y: " + y + "\r");
            //System.out.println("Y: " + y);
            int pixel = image.getRGB(y, x);
            r = (pixel >> 16) & 0xff;
            g = (pixel >> 8) & 0xff;
            b = (pixel) & 0xff;

            XSSFCell cell1 = row1.createCell(y);
            XSSFCell cell2 = row2.createCell(y);
            XSSFCell cell3 = row3.createCell(y);

            cell1.setCellValue(Integer.toString(r));
            cell2.setCellValue(Integer.toString(g));
            cell3.setCellValue(Integer.toString(b));

            style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(r, 0, 0)));
            ;
            style1.setFillPattern(CellStyle.SOLID_FOREGROUND);

            style2.setFillForegroundColor(new XSSFColor(new java.awt.Color(0, g, 0)));
            style2.setFillPattern(CellStyle.SOLID_FOREGROUND);

            style3.setFillForegroundColor(new XSSFColor(new java.awt.Color(0, 0, b)));
            style3.setFillPattern(CellStyle.SOLID_FOREGROUND);

            cell1.setCellStyle(style1);
            cell2.setCellStyle(style2);
            cell3.setCellStyle(style3);

            //System.out.println("x,y: " + j + ", " + i);
            //System.out.println("R: " + r + " G: " + g + " B: " + b + "\n");
        }
    }
    System.out.println("RGB values extracted.");
    System.out.println("Generating image");

    myExcel.write(new FileOutputStream("excel.xlsx"));
    myExcel.close();

}

From source file:ke.co.tawi.babblesms.server.servlet.export.excel.inbox.ExportExcel.java

License:Open Source License

/**
 * Returns MS Excel file of the data specified for exporting.
 * @param List<IncomingLog>//w w  w  .  j  a  v a 2  s . c  o  m
 * Method create excelSheets and sends them
 ****/
public void createExcelSheets(List<IncomingLog> InLog) throws IOException {
    List<Phone> phoneList;
    //String cont = null;

    XSSFWorkbook xf = new XSSFWorkbook();
    XSSFCreationHelper ch = xf.getCreationHelper();

    XSSFSheet s = xf.createSheet();
    //create the first row
    XSSFRow r1 = s.createRow(0);
    XSSFCell c11 = r1.createCell(0);
    c11.setCellValue(ch.createRichTextString("*"));
    XSSFCell c12 = r1.createCell(1);
    c12.setCellValue(ch.createRichTextString("Message"));
    XSSFCell c13 = r1.createCell(2);
    c13.setCellValue(ch.createRichTextString("Source"));
    XSSFCell c14 = r1.createCell(3);
    c14.setCellValue(ch.createRichTextString("Destination"));
    XSSFCell c15 = r1.createCell(4);
    c15.setCellValue(ch.createRichTextString("Network"));
    XSSFCell c16 = r1.createCell(5);
    c16.setCellValue(ch.createRichTextString("Time (" + timezoneFormatter.format(new Date()) + ") Time Zone"));
    XSSFCell c17 = r1.createCell(6);
    c17.setCellValue(ch.createRichTextString("Message Id"));

    int i = 1;
    //create other rows
    for (IncomingLog log : InLog) {
        phoneList = phnDAO.getPhones(log.getOrigin());

        XSSFRow r = s.createRow(i);
        //row number
        XSSFCell c1 = r.createCell(0);
        c1.setCellValue(i + pageno);

        //get message  
        XSSFCell c2 = r.createCell(1);
        c2.setCellValue(ch.createRichTextString(log.getMessage()));

        //get phone numbers
        XSSFCell c3 = r.createCell(2);
        if (phoneList.size() > 0) {
            for (Phone phone : phoneList) {
                Contact contacts = ctDAO.getContact(phone.getContactUuid());
                c3.setCellValue(ch.createRichTextString(contacts.getName()));
            }
        } else {
            c3.setCellValue(ch.createRichTextString(log.getOrigin()));
        }

        //get destination   
        XSSFCell c4 = r.createCell(3);
        c4.setCellValue(ch.createRichTextString(log.getDestination()));

        //get network name    
        XSSFCell c5 = r.createCell(4);
        c5.setCellValue(ch.createRichTextString(networkHash.get(log.getNetworkUuid())));

        //get date 
        XSSFCell c6 = r.createCell(5);
        c6.setCellValue(ch.createRichTextString("" + dateFormatter.format(log.getLogTime())));

        //get message id
        XSSFCell c7 = r.createCell(6);
        c7.setCellValue(ch.createRichTextString(log.getUuid()));
        i++;

    }
    xf.write(out);
    out.flush();
    out.close();
}

From source file:kp.servlet.ExportRpt.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  w ww .  jav a  2 s  .com*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    Logger.getLogger(ExportRpt.class.getName()).log(Level.SEVERE,
            "accRole :" + request.getParameter("accRole"));
    Logger.getLogger(ExportRpt.class.getName()).log(Level.SEVERE, "Unit :" + request.getParameter("unit"));
    ArrayList<MocWfTran> Mocstatus = new ArrayList<>();
    TranDao tdao = new TranDao();
    Mocstatus = tdao.getMocStatusList(request.getParameter("accRole"), request.getParameter("unit"),
            request.getParameter("user"));

    //Developing Metadata
    String rptName = "MOC Status Excel Report";
    ArrayList<String> colLabel = new ArrayList<>();
    colLabel.add("Case Id");
    colLabel.add("Moc NO");
    colLabel.add("Moc Title");
    colLabel.add("Moc Status");
    colLabel.add("Creation Date");
    colLabel.add("Owner's Name");
    colLabel.add("Unit");
    colLabel.add("Plant");
    colLabel.add("Current Stage");
    colLabel.add("Pending At");

    //Starting EXCEL Creating
    //XLS Variable
    XSSFSheet spreadsheet;
    XSSFWorkbook workbook;
    XSSFRow row;
    XSSFCell cell;
    XSSFFont xfont = null;
    XSSFCellStyle xstyle = null;

    //2.Create WorkBook and Sheet
    workbook = new XSSFWorkbook();
    spreadsheet = workbook.createSheet(rptName);

    //set header style
    xfont = workbook.createFont();
    xfont.setFontHeight(11);
    xfont.setFontName("Calibri");
    xfont.setBold(true);

    //Set font into style
    CellStyle borderStyle = workbook.createCellStyle();
    borderStyle.setAlignment(CellStyle.ALIGN_CENTER);
    borderStyle.setFont(xfont);
    xstyle = workbook.createCellStyle();
    xstyle.setFont(xfont);

    //header
    row = spreadsheet.createRow(0);
    cell = row.createCell(0);
    cell.setCellValue(rptName);
    cell.setCellStyle(borderStyle);
    spreadsheet.addMergedRegion(new CellRangeAddress(0, 0, 0, colLabel.size() - 1));

    //3.Get First Row and Set Headers
    row = spreadsheet.createRow(1);

    for (int i = 0; i < colLabel.size(); i++) {
        cell = row.createCell(i);
        cell.setCellValue(colLabel.get(i));
        cell.setCellStyle(xstyle);
    }

    //Itrate or Database data and write
    int i = 2;
    for (MocWfTran bean : Mocstatus) {
        row = spreadsheet.createRow(i);
        cell = row.createCell(0);
        cell.setCellValue(bean.getCaseId());
        cell = row.createCell(1);
        cell.setCellValue(bean.getMocNo());
        cell = row.createCell(2);
        cell.setCellValue(bean.getCaseName());
        cell = row.createCell(3);
        cell.setCellValue(bean.getMocStatus());
        cell = row.createCell(4);
        cell.setCellValue(bean.getCrDateString());
        cell = row.createCell(5);
        cell.setCellValue(bean.getCaseOwnerName());
        cell = row.createCell(6);
        cell.setCellValue(bean.getUnitId());
        cell = row.createCell(7);
        cell.setCellValue(bean.getPlantId());
        cell = row.createCell(8);
        cell.setCellValue(bean.getStgNname());
        cell = row.createCell(9);
        cell.setCellValue(bean.getUserNname());
        i++;
    }

    //Export to Excel
    String file_name = "MocStatus";
    String path = getServletContext().getRealPath("/");
    String full_path = path + "/report/" + file_name + ".xlsx";
    //        FileOutputStream out = new FileOutputStream(new File("D://" + file_name + ".xlsx"));
    FileOutputStream out = new FileOutputStream(new File(full_path));
    workbook.write(out);

    //Download code 
    // reads input file from an absolute path
    File downloadFile = new File(full_path);
    OutputStream outStream;
    // obtains ServletContext
    try (FileInputStream inStream = new FileInputStream(downloadFile)) {
        //obtains ServletContext
        ServletContext context = getServletContext();
        // gets MIME type of the file
        String mimeType = context.getMimeType(full_path);
        if (mimeType == null) {
            // set to binary type if MIME mapping not found
            mimeType = "application/octet-stream";
        } // modifies response
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());
        // forces download
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
        response.setHeader(headerKey, headerValue);
        // obtains response's output stream
        outStream = response.getOutputStream();
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }
    }
    outStream.close();
    //        response.sendRedirect("mocstatus.jsp");
}

From source file:localization.excel.java

public static void convert(String filePath) {
    Vector<String> zFile;
    if (filePath.endsWith(".zip")) {
        zFile = readzipfile(filePath);/*from w ww  .ja  v a2 s  .  c  om*/
        for (String s : zFile) {
            if (s.endsWith(".xlsx")) {
                //System.out.println(s);
                convert(s);
            }
        }
    } else if (!filePath.endsWith(".xlsx")) {
        return;
    } else {
        try {
            FileInputStream file = new FileInputStream(new File(filePath));
            System.out.println(filePath);
            //Get the workbook instance for XLS file 
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            XSSFSheet sheet = workbook.getSheetAt(0);
            XSSFRow row;
            XSSFCell cell;
            rowNumber = sheet.getPhysicalNumberOfRows();
            try {
                for (int i = 0; i < rowNumber; i++) {
                    row = sheet.getRow(i);
                    if (row != null) {
                        int columnNum = row.getPhysicalNumberOfCells();
                        //System.out.println(columnNum);
                        for (int j = 0; j < columnNum; j++) {
                            cell = row.getCell(j);

                            if (j == 0) {
                                String name = cell.getRichStringCellValue().getString();
                                if (name.equalsIgnoreCase("Esri")) {
                                    langNumber++;
                                }
                                //System.out.println(name);
                            }
                        }
                        if (i == 3) {
                            cell = row.getCell(30);
                            XSSFCellStyle cs = cell.getCellStyle();
                            cell = row.createCell(32);
                            cell.setCellValue("Additional Charge per language");
                            cell.setCellStyle(cs);
                        }
                    }
                }
            } catch (Exception e) {

            }
            System.out.println(langNumber);
            double total = Double.parseDouble(sheet.getRow(langNumber + 3).getCell(29).getRawValue());

            double subTotal = total / langNumber;
            DecimalFormat df = new DecimalFormat("#.000");
            for (int i = 0; i < langNumber; i++) {
                cell = sheet.getRow(i + 4).createCell(32);
                cell.setCellValue("$" + df.format(subTotal));
            }

            file.close();
            FileOutputStream outFile = new FileOutputStream(filePath);
            workbook.write(outFile);
            outFile.close();
            rowNumber = 0;
            langNumber = 0;
            System.out.println("Done");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

From source file:Logic.RStoXL.java

public void genXLS(ResultSet rs, String Rpt_name, String path) {
    try {/*from  w  w w  .jav a  2  s.  c  o  m*/
        //RS METE DATA
        ResultSetMetaData rsmd = rs.getMetaData();
        int col_count = rsmd.getColumnCount();
        ArrayList<String> col_name = new ArrayList<String>();
        for (int i = 1; i <= col_count; i++) {
            col_name.add(rsmd.getColumnLabel(i));
        }

        //XLS Variable
        XSSFSheet spreadsheet;
        XSSFWorkbook workbook;
        XSSFRow row;
        XSSFCell cell;
        XSSFFont xfont = null;
        XSSFCellStyle xstyle = null;

        //2.Create WorkBook and Sheet
        workbook = new XSSFWorkbook();
        spreadsheet = workbook.createSheet(Rpt_name);

        //set header style
        xfont = workbook.createFont();
        xfont.setFontHeight(11);
        xfont.setFontName("Calibri");
        xfont.setBold(true);

        //Set font into style
        CellStyle borderStyle = workbook.createCellStyle();
        borderStyle.setAlignment(CellStyle.ALIGN_CENTER);
        borderStyle.setFont(xfont);
        xstyle = workbook.createCellStyle();
        xstyle.setFont(xfont);

        //header
        row = spreadsheet.createRow(0);
        cell = row.createCell(0);
        cell.setCellValue(Rpt_name);
        cell.setCellStyle(borderStyle);
        spreadsheet.addMergedRegion(new CellRangeAddress(0, 0, 0, col_count - 1));

        //3.Get First Row and Set Headers
        row = spreadsheet.createRow(1);

        for (int i = 0; i < col_count; i++) {
            cell = row.createCell(i);
            cell.setCellValue(col_name.get(i));
            cell.setCellStyle(xstyle);
        }

        //Itrate or Database data and write
        int i = 2;
        while (rs.next()) {
            row = spreadsheet.createRow(i);
            for (int j = 1; j <= col_count; j++) {
                cell = row.createCell(j - 1);
                cell.setCellValue(rs.getString(j));
            }
            i++;
        }

        //Export to Excel
        // FileOutputStream out = new FileOutputStream(new File("D://" + Rpt_name + ".xlsx"));
        FileOutputStream out = new FileOutputStream(new File(path));
        workbook.write(out);

        Logger.getLogger(RStoXL.class.getName()).log(Level.SEVERE, "DONE|!");
        Logger.getLogger(RStoXL.class.getName()).log(Level.SEVERE, "");
    } catch (SQLException ex) {
        Logger.getLogger(RStoXL.class.getName()).log(Level.SEVERE, "Exception : {0}", ex);
    } catch (IOException ex) {
        Logger.getLogger(RStoXL.class.getName()).log(Level.SEVERE, "Exception : {0}", ex);
    }
}