Results 1 to 4 of 4
  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Smile (in Java mysql to excel) where i have specify the path to save the file

    Good Morning!

    Successfully i converted data from mysql to excel by using java

    the converted file is storing only on default location (inside the package)

    i want to store sum where (eg; on desktop)

    help me, if i coding with JFileChooser. where & how to pass the filename according to this code..,

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package excel;
    
    /**
     *
     * @author raj
     */
    import java.io.*;
    import java.sql.*;
    import org.apache.poi.hssf.usermodel.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    public class MysqlToXls 
    {
      public MysqlToXls()throws ClassNotFoundException, SQLException 
      {
          try
                    {
                        HSSFWorkbook xlsWorkbook = new HSSFWorkbook();
                        HSSFSheet xlsSheet = xlsWorkbook.createSheet();
                        short rowIndex = 0;
                             Class.forName("com.mysql.jdbc.Driver");
                             Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/payslip", "root", "root");
                             PreparedStatement stmt = conn.prepareStatement("select * from salarydet where EmployeeId ='"+108+"'");
                             ResultSet rs = stmt.executeQuery();
                        ResultSetMetaData colInfo = rs.getMetaData();
                        List<String> colNames = new ArrayList<String>();
                        HSSFRow titleRow = xlsSheet.createRow(rowIndex++);
    
        for (int i = 1; i <= colInfo.getColumnCount(); i++) 
                     {
                colNames.add(colInfo.getColumnName(i));
                titleRow.createCell((short) (i-1)).setCellValue(
                new HSSFRichTextString(colInfo.getColumnName(i)));
                xlsSheet.setColumnWidth((short) (i-1), (short) 4000);
        }
    
        while (rs.next()) 
        {
          HSSFRow dataRow = xlsSheet.createRow(rowIndex++);
          short colIndex = 0;
          for (String colName : colNames) {
              dataRow.createCell(colIndex++).setCellValue(
              new HSSFRichTextString(rs.getString(colName)));
          }
        }
        xlsWorkbook.write(new FileOutputStream("SUCCESS.xls"));
        conn.close();
                                              
                    }
                    catch(Exception r)
                    {
                        JOptionPane.showMessageDialog(null, r);
                    }
      }
      public static void main(String[] args) 
      {
        try {
          MysqlToXls mysqlToXls = new MysqlToXls();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

    Thank you sir..,

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: (in Java mysql to excel) where i have specify the path to save the file

    What about:

    Java Code:
    xlsWorkbook.write(new FileOutputStream(new File("/path/to/your/desktop/SUCCESS.xls")));

  3. #3
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default Re: (in Java mysql to excel) where i have specify the path to save the file

    thank you very much sir..,

  4. #4
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default Re: (in Java mysql to excel) where i have specify the path to save the file

    it is working well..,
    Thank you sir..,

Similar Threads

  1. Upload file in jsp and save it to MYSQL
    By juhiswt in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 05-03-2012, 07:38 AM
  2. Java save to file / open from file - trouble
    By joelBeaton in forum Advanced Java
    Replies: 6
    Last Post: 03-23-2012, 12:47 PM
  3. Replies: 0
    Last Post: 11-17-2011, 09:33 AM
  4. Replies: 0
    Last Post: 04-02-2011, 10:22 AM
  5. Save data from Excel sheet to servlet
    By chandu.v in forum Java Servlet
    Replies: 2
    Last Post: 04-10-2008, 01:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •