(in Java mysql to excel) where i have specify the path to save the file
Good Morning! :(handshake):
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..,
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..,
Re: (in Java mysql to excel) where i have specify the path to save the file
What about:
Code:
xlsWorkbook.write(new FileOutputStream(new File("/path/to/your/desktop/SUCCESS.xls")));
Re: (in Java mysql to excel) where i have specify the path to save the file
thank you very much sir..,
Re: (in Java mysql to excel) where i have specify the path to save the file
it is working well..,
Thank you sir..,