Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Writing to Excel

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    15
    My Mood
    Cold
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing to Excel

    I'm having trouble writing to an Excel document using jexcelapi. What I'm trying to do is write a program that has users put in their name and ID and this, along with the date and time is automatically inserted into an Excel document. The problem that I'm having is breaking down the sample code (I will post in the comments) to use the pieces that I need, or writing my own code that writes to Excel by itself. I have tested the sample code, it works, and I have tested the part of my code, it also works, but when combining the two, everything breaks down. I have been through many Youtube videos and tutorials and none provide ample guide as to how this could be done. I am also considering switching to Apache API if this doesn't work so if anyone has any suggestions, they would be much appreciated.

    I keep getting denied when I try to post my code...so I will try and post it in the comments...

    --- Update ---

    package Writer;

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    import java.util.Locale;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;

    import jxl.CellView;
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.format.UnderlineStyle;
    import jxl.write.Formula;
    import jxl.write.Label;
    import jxl.write.Number;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import jxl.write.biff.RowsExceededException;


    public class Excel extends JFrame implements ActionListener {

    private WritableCellFormat timesBoldUnderline;
    private WritableCellFormat times;
    private String inputFile;

    public void setOutputFile(String inputFile) {
    this.inputFile = inputFile;
    }

    public void write() throws IOException, WriteException {
    File file = new File(inputFile);
    WorkbookSettings wbSettings = new WorkbookSettings();

    wbSettings.setLocale(new Locale("en", "EN"));

    WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
    workbook.createSheet("Report", 0);
    WritableSheet excelSheet = workbook.getSheet(0);
    createLabel(excelSheet);
    createContent(excelSheet);

    workbook.write();
    workbook.close();
    }

    private void createLabel(WritableSheet sheet)
    throws WriteException {
    // Lets create a times font
    WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
    // Define the cell format
    times = new WritableCellFormat(times10pt);
    // Lets automatically wrap the cells
    times.setWrap(true);

    // create create a bold font with unterlines
    WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false,
    UnderlineStyle.SINGLE);
    timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
    // Lets automatically wrap the cells
    timesBoldUnderline.setWrap(true);

    CellView cv = new CellView();
    cv.setFormat(times);
    cv.setFormat(timesBoldUnderline);
    cv.setAutosize(true);

    // Write a few headers
    addCaption(sheet, 0, 0, "User");
    addCaption(sheet, 1, 0, "ID");
    addCaption(sheet, 2, 0, "LogIn Time");
    addCaption(sheet, 3, 0, "LogOut Time");

    }

    private void createContent(WritableSheet sheet) throws WriteException,
    RowsExceededException
    {
    JFrame frame = new JFrame();
    frame.setLocationRelativeTo(null);
    frame.setSize(200, 105);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setLayout(new FlowLayout());
    JButton button = new JButton("LogIn");
    JButton button2 = new JButton("LogOut");
    JButton button3 = new JButton("Exit");
    button.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    frame.add(button);
    frame.add(button2);
    frame.add(button3);
    }
    @Override
    public void actionPerformed(ActionEvent e)
    {
    String name = e.getActionCommand();
    if(name.equals("LogIn"))
    {
    WritableWorkbook workbook = Workbook.createWorkbook(new File("F:/Excel/User.xls");
    {
    WritableSheet sheet = workbook.createSheet("First Sheet", 0);
    String x = JOptionPane.showInputDialog(null, "User");
    JOptionPane.showMessageDialog(null, x);
    addCaption(sheet, 1, 0, x);
    }
    }

    else if(name.equals("LogOut"))
    {
    String y = JOptionPane.showInputDialog(null, "ID Number");
    JOptionPane.showMessageDialog(null, y);
    }
    else if(name.equals("Exit"))
    {
    System.exit(0);
    }


    }

    /* // Write a few number
    for (int i = 1; i < 10; i++) {
    // First column
    addNumber(sheet, 0, i, i + 10);
    // Second column
    addNumber(sheet, 1, i, i * i);
    }
    // Lets calculate the sum of it
    StringBuffer buf = new StringBuffer();
    buf.append("SUM(A2:A10)");
    Formula f = new Formula(0, 10, buf.toString());
    sheet.addCell(f);
    buf = new StringBuffer();
    buf.append("SUM(B2:B10)");
    f = new Formula(1, 10, buf.toString());
    sheet.addCell(f);

    // now a bit of text
    for (int i = 12; i < 20; i++) {
    // First column
    addLabel(sheet, 0, i, "Boring text " + i);
    // Second column
    addLabel(sheet, 1, i, "Another text");
    }*/


    private void addCaption(WritableSheet sheet, int column, int row, String s)
    throws RowsExceededException, WriteException {
    Label label;
    label = new Label(column, row, s, timesBoldUnderline);
    sheet.addCell(label);
    }

    private void addNumber(WritableSheet sheet, int column, int row,
    Integer integer) throws WriteException, RowsExceededException {
    Number number;
    number = new Number(column, row, integer, times);
    sheet.addCell(number);
    }

    private void addLabel(WritableSheet sheet, int column, int row, String s)
    throws WriteException, RowsExceededException {
    Label label;
    label = new Label(column, row, s, times);
    sheet.addCell(label);
    }

    public static void main(String[] args) throws WriteException, IOException {
    Excel test = new Excel();
    test.setOutputFile("F:/Excel/TEST.xls" );
    test.write();
    System.out
    .println("Please check the result file under F:/Excel/TEST.xls ");
    }
    }
    I Highlighted my code, the rest is from the sample.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Writing to Excel

    Please post your code using code or highlight tags as explained near the top of this topic.

Similar Threads

  1. consolidate all the excel sheets in to one excel sheet
    By nakka07438 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2014, 11:35 PM
  2. How to Remove Hype link from Excel while exporting to excel.
    By Rakesh Bhat in forum Java Theory & Questions
    Replies: 0
    Last Post: October 9th, 2013, 09:11 AM
  3. Reading from a Text file & writing to Excel spreadsheets
    By javanooby in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 14th, 2012, 12:41 PM
  4. [SOLVED] Excel Report
    By banny7 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 18th, 2011, 10:27 AM
  5. Replies: 3
    Last Post: September 1st, 2011, 10:49 AM