Ver Mensaje Individual
  #2 (permalink)  
Antiguo 19/02/2012, 11:01
CiberPrograming
 
Fecha de Ingreso: febrero-2012
Mensajes: 3
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: Expotar e Importar Msql A Un Excel XLS

public class MysqlToXls {

private Connection connection = null;
private String driver = "com.mysql.jdbc.Driver";

public MysqlToXls(String database, String user, String password)
throws ClassNotFoundException, SQLException {

Class.forName(driver);
String url = "jdbc:mysql://localhost:3306/" + "El nombre q su BD";
connection = DriverManager.getConnection(url, user, password);

}

public void generateXls(String tablename, String filename)
throws SQLException, FileNotFoundException, IOException {


HSSFWorkbook xlsWorkbook = new HSSFWorkbook();
HSSFSheet xlsSheet = xlsWorkbook.createSheet();
short rowIndex = 0;


PreparedStatement stmt =
connection.prepareStatement("select * from " + "El nombre de su tabla");

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(filename));
}


public void close() throws SQLException {

connection.close();
}

public static void main(String[] args) {
try {

MysqlToXls mysqlToXls = new MysqlToXls("test", "root", "La contraseña si tuviesen");
mysqlToXls.generateXls("Ejmplo", "Ejmplo.xls");
mysqlToXls.close();
} catch (Exception e) {

e.printStackTrace();

}

}


Bueno esto investigue y me funciona pero me lo guarda en el mismos proyecto en su carpeta pero
yo quiero saber como decidir donde guardarlo tu mismo XD helpp!!