Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing to excel in java

Can someone point me in the right direction for writing to an excel file in java?? I am not understanding the links I found online. Could you just send me a link or anything which I could follow through??

Thank you, J

like image 478
JJunior Avatar asked Aug 11 '10 03:08

JJunior


People also ask

How do you write CSV file in Excel using Java?

Convert CSV to Excel (XLSX/XLS) using JavaInitialize LoadOptions class object. Specify FileFormatType as CSV. Instantiate an object of Workbook class. Save the output Excel file.

How do I write data in Excel?

Enter text or a number in a cellOn the worksheet, click a cell. Type the numbers or text that you want to enter, and then press ENTER or TAB. To enter data on a new line within a cell, enter a line break by pressing ALT+ENTER.

Can Java work with Excel?

Especially when working with loads of data, Excel makes it pretty easy to order, filter, calculate, visualize and report. Expanding into loads^2 of data and adding automation on top is also relatively simple: just couple Excel with Java to do the hard work of mining databases and delivering clean sheets.


2 Answers

Another alternative to Apache POI is the JExcelAPI, which (IMO) has an easier to use API. Some examples:

WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls"));

WritableSheet sheet = workbook.createSheet("First Sheet", 0);

Label label = new Label(0, 2, "A label record"); 
sheet.addCell(label); 

Number number = new Number(3, 4, 3.1459); 
sheet.addCell(number);
like image 186
matt b Avatar answered Sep 25 '22 13:09

matt b


public class ExcelUtils {

    private static XSSFSheet ExcelWSheet;
    private static XSSFWorkbook ExcelWBook;
    private static XSSFCell Cell;
    private static XSSFRow Row;
    File fileName = new File("C:\\Users\\satekuma\\Pro\\Fund.xlsx");

    public void setExcelFile(File Path, String SheetName) throws Exception {
        try {
            FileInputStream ExcelFile = new FileInputStream(Path);
            ExcelWBook = new XSSFWorkbook(ExcelFile);
            ExcelWSheet = ExcelWBook.getSheet(SheetName);
        } catch (Exception e) {
            throw (e);
        }
    }


    public static String getCellData(int RowNum, int ColNum) throws      Exception {
        try {
            Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
            String CellData = Cell.getStringCellValue();
            return CellData;
        } catch (Exception e) {
            return "";
        }
    }

    public static void setCellData(String Result, int RowNum, int ColNum, File Path) throws Exception {
        try {
            Row = ExcelWSheet.createRow(RowNum - 1);
            Cell = Row.createCell(ColNum - 1);
            Cell.setCellValue(Result);
            FileOutputStream fileOut = new FileOutputStream(Path);
            ExcelWBook.write(fileOut);
            fileOut.flush();
            fileOut.close();
        } catch (Exception e) {
            throw (e);
        }
    }
}
like image 39
satender Avatar answered Sep 25 '22 13:09

satender