Created
May 19, 2011 04:29
-
-
Save khannedy/980186 to your computer and use it in GitHub Desktop.
Java Converter to Microsoft Excel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2011 Eko Kurniawan Khannedy. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* under the License. | |
*/ | |
package com.stripbandunk.jdbctoexcel.converter; | |
import com.stripbandunk.jdbctoexcel.error.UpsErrorException; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import org.apache.poi.hssf.usermodel.HSSFRow; | |
import org.apache.poi.hssf.usermodel.HSSFSheet; | |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
/** | |
* | |
* @author Eko Kurniawan Khannedy | |
*/ | |
public class ExcelConverter { | |
private ExcelRowFiller filler; | |
public ExcelConverter(ExcelRowFiller filler) { | |
this.filler = filler; | |
} | |
public void save(File file) { | |
HSSFWorkbook workbook = new HSSFWorkbook(); | |
HSSFSheet sheet = workbook.createSheet(); | |
for (int i = 0; i < filler.totalRow(); i++) { | |
HSSFRow row = sheet.createRow(i); | |
filler.fill(row, i); | |
} | |
FileOutputStream stream = null; | |
try { | |
stream = new FileOutputStream(file); | |
workbook.write(stream); | |
} catch (IOException ex) { | |
throw new UpsErrorException(ex); | |
} finally { | |
if (stream != null) { | |
try { | |
stream.close(); | |
} catch (IOException ex) { | |
throw new UpsErrorException(ex); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2011 Eko Kurniawan Khannedy. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* under the License. | |
*/ | |
package com.stripbandunk.jdbctoexcel.converter; | |
import org.apache.poi.hssf.usermodel.HSSFRow; | |
/** | |
* | |
* @author Eko Kurniawan Khannedy | |
*/ | |
public interface ExcelRowFiller { | |
void fill(HSSFRow row, int index); | |
int totalRow(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2011 Eko Kurniawan Khannedy. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* under the License. | |
*/ | |
package com.stripbandunk.jdbctoexcel.converter.jdbc; | |
import com.stripbandunk.jdbctoexcel.converter.ExcelRowFiller; | |
import com.stripbandunk.jdbctoexcel.error.UpsErrorException; | |
import java.sql.ResultSet; | |
import java.sql.ResultSetMetaData; | |
import java.sql.SQLException; | |
import org.apache.poi.hssf.usermodel.HSSFCell; | |
import org.apache.poi.hssf.usermodel.HSSFRow; | |
/** | |
* | |
* @author Eko Kurniawan Khannedy | |
*/ | |
public class JdbcRowFiller implements ExcelRowFiller { | |
private ResultSet resultSet; | |
private ResultSetMetaData metaData; | |
public JdbcRowFiller(ResultSet resultSet) { | |
try { | |
this.resultSet = resultSet; | |
metaData = resultSet.getMetaData(); | |
} catch (SQLException ex) { | |
throw new UpsErrorException(ex); | |
} | |
} | |
public void fill(HSSFRow row, int index) { | |
try { | |
resultSet.absolute(index + 1); | |
for (int i = 0; i < metaData.getColumnCount(); i++) { | |
HSSFCell cell = row.createCell(i); | |
cell.setCellValue(resultSet.getString(metaData.getColumnName(i + 1))); | |
} | |
} catch (SQLException ex) { | |
throw new UpsErrorException(ex); | |
} | |
} | |
public int totalRow() { | |
try { | |
resultSet.last(); | |
return resultSet.getRow(); | |
} catch (SQLException ex) { | |
throw new UpsErrorException(ex); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!(fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)) { | |
return; | |
} | |
File file = fileChooser.getSelectedFile(); | |
if (!file.getPath().endsWith(".xls")) { | |
file = new File(file.getPath() + ".xls"); | |
} | |
String url = "jdbc:mysql://" + textHost.getText() + ":" + textPOrt.getText() + "/" + textDatabase.getText(); | |
System.out.println(url); | |
String username = textUsername.getText(); | |
String password = new String(textPassword.getPassword()); | |
String sql = "select * from `" + textTable.getText() + "`"; | |
Connection connection = null; | |
Statement statement = null; | |
ResultSet resultSet = null; | |
try { | |
connection = DriverManager.getConnection(url, username, password); | |
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); | |
resultSet = statement.executeQuery(sql); | |
ExcelRowFiller filler = new JdbcRowFiller(resultSet); | |
ExcelConverter converter = new ExcelConverter(filler); | |
converter.save(file); | |
} catch (SQLException ex) { | |
throw new UpsErrorException(ex); | |
} finally { | |
DbUtils.closeQuietly(connection, statement, resultSet); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment