Advertisement
Guest User

Untitled

a guest
Jul 8th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import javax.swing.JOptionPane;
  4. import org.apache.commons.io.FileUtils;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.Statement;
  11. import java.sql.SQLException;
  12. import org.apache.poi.xssf.usermodel.XSSFCell;
  13. import org.apache.poi.xssf.usermodel.XSSFRow;
  14. import org.apache.poi.xssf.usermodel.XSSFSheet;
  15. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  16.  
  17. public class ExportToExcel {
  18. {
  19.  
  20. public static void main(String args[]) throws IOException {
  21. {
  22. String source = "C:/Test/DATABASE.db";
  23. String target ="C:/Test/COPY_";
  24.  
  25.  
  26. File sourceFile = new File(source);
  27. String name = sourceFile.getName();
  28.  
  29. File targetFile = new File(target+name);
  30. System.out.println("Copying file : " + sourceFile.getName());
  31.  
  32.  
  33. FileUtils.copyFile(sourceFile, targetFile);
  34.  
  35. System.out.println("copying of file:"+ sourceFile.getName() +"is completed");
  36.  
  37. Connection connection = null;
  38. //@Override
  39. //public Connection getConection() throws SQLException {
  40.  
  41.  
  42. try {
  43. Class.forName("org.sqlite.JDBC");
  44.  
  45.  
  46. connection = DriverManager.getConnection("jdbc:sqlite:" + "C:/Test/DATABASE.db");
  47. } catch (SQLException e) {
  48. throw new SQLException(e.getMessage());
  49. } catch (Exception e) {
  50. throw new SQLException(e.getCause());
  51. }
  52. // connection = DriverManager.getConnection("jdbc:sqlite:" + "C:/Test/COPY_DATABASE.db"); //connection = DriverManager.getConnection("jdbc:sqlite:" + "resources\COPY_TRACARGODB.db");
  53. Statement statement = connection.createStatement();
  54. ResultSet resultSet = statement
  55. .executeQuery("select * from MAIN_TABLE");
  56. XSSFWorkbook workbook = new XSSFWorkbook();
  57. XSSFSheet spreadsheet = workbook
  58. .createSheet("Tracargo EXPORTED DB");
  59. XSSFRow row=spreadsheet.createRow(1);// should start at 0 not one.. But this is ok for testing..
  60. XSSFCell cell;
  61. cell=row.createCell(1);// should start at 0 not one.. But this is ok for testing..
  62. cell.setCellValue("Number");
  63. cell=row.createCell(2);
  64. cell.setCellValue("First Name");
  65. cell=row.createCell(3);
  66. cell.setCellValue("Last Name");
  67. cell=row.createCell(4);
  68. cell.setCellValue("Origin");
  69. cell=row.createCell(5);
  70. cell.setCellValue("Destination");
  71. cell=row.createCell(6);
  72. //
  73. cell.setCellValue("Arrival");
  74. cell=row.createCell(7);
  75. cell.setCellValue("Departure");
  76. cell=row.createCell(8);
  77. cell.setCellValue("Status");
  78. cell=row.createCell(9);
  79. cell.setCellValue("Created");
  80. cell=row.createCell(10);
  81. cell.setCellValue("Updated");
  82. cell=row.createCell(11);
  83. cell.setCellValue("Message");
  84. cell=row.createCell(12);
  85. cell.setCellValue("Active");
  86. //
  87. int i=2;
  88. while(resultSet.next())
  89. {
  90. row=spreadsheet.createRow(i);
  91. cell=row.createCell(1);
  92. cell.setCellValue(resultSet.getInt("number"));
  93. cell=row.createCell(2);
  94. cell.setCellValue(resultSet.getString("first_name"));
  95. cell=row.createCell(3);
  96. cell.setCellValue(resultSet.getString("last_name"));
  97. cell=row.createCell(4);
  98. cell.setCellValue(resultSet.getString("origin"));
  99. cell=row.createCell(5);
  100. cell.setCellValue(resultSet.getString("destination"));
  101. //
  102. cell=row.createCell(6);
  103. cell.setCellValue(resultSet.getString("arrival"));
  104. cell=row.createCell(7);
  105. cell.setCellValue(resultSet.getString("departure"));
  106. cell=row.createCell(8);
  107. cell.setCellValue(resultSet.getString("status"));
  108. cell=row.createCell(9);
  109. cell.setCellValue(resultSet.getString("created"));
  110. cell=row.createCell(10);
  111. cell.setCellValue(resultSet.getString("updated"));
  112. cell=row.createCell(11);
  113. cell.setCellValue(resultSet.getString("message"));
  114. cell=row.createCell(12);
  115. cell.setCellValue(resultSet.getString("active"));
  116. //
  117. i++;
  118. }
  119. FileOutputStream out = new FileOutputStream(
  120. new File("exceldatabase.xlsx"));
  121. workbook.write(out);
  122. out.close();
  123. // workbook.close();
  124. System.out.println(
  125. "exceldatabase.xlsx written successfully");
  126.  
  127. {
  128. try{
  129.  
  130. File filetodelete = new File("C:/Test/COPY_DATABASE.db");
  131.  
  132. if(filetodelete.delete()){
  133. System.out.println(filetodelete.getName() + " is deleted!");
  134. }else{
  135. System.out.println("Delete has failed.");
  136.  
  137. }
  138.  
  139. }catch(Exception e){
  140.  
  141. e.printStackTrace();
  142. }
  143. }
  144. }
  145. }
  146. }
  147.  
  148. Exception in thread "main" java.lang.Error: Unresolved compilation problem:
  149.  
  150. at ExportToExcel.main(ExportToExcel.java:20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement