Advertisement
Guest User

Untitled

a guest
Jan 15th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. sheet.setAutoFilter(new CellRangeAddress(firstCell.getRow(), lastCell.getRow(), firstCell.getCol(), lastCell.getCol()));
  2.  
  3. >x (x, y)
  4. 0123456
  5. 0|--hhh--| h = header
  6. 1|--+++--| + = values
  7. 2|--+++--| - = empty fields
  8. 3|--+++--|
  9. 4|-------|
  10.  
  11. //include poi-3.7.jar,ojdbc.jar in classpath
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.ResultSet;
  15. import java.sql.ResultSetMetaData;
  16. import java.sql.Statement;
  17. import java.io.*;
  18. import java.net.*;
  19. import javax.servlet.*;
  20. import javax.servlet.http.*;
  21. import org.apache.poi.hssf.usermodel.*;
  22. import org.apache.poi.hssf.util.HSSFColor;
  23. import org.apache.poi.ss.usermodel.Cell;
  24. import org.apache.poi.ss.usermodel.CellStyle;
  25. import org.apache.poi.ss.usermodel.IndexedColors;
  26.  
  27. public class ExcelCreator{
  28. public static void createExcel(String anySql,OutputStream out) throws Exception{
  29. Connection conn=getOracleConnection();
  30. String userSql1="select * from tab";
  31. String userSql2="select * from tab";
  32. String[] sqls={userSql1,userSql2,anySql};
  33. String[] workSheetNames={"User","Manager","Any"};
  34. HSSFWorkbook wb = new HSSFWorkbook();
  35. CellStyle style1 = wb.createCellStyle();
  36. CellStyle style2 = wb.createCellStyle();
  37. for(int i=0;i<lt;sqls.length;i++){
  38. if(sqls[i]==null||sqls[i].length()==0){
  39. continue;
  40. }
  41. HSSFSheet sheet = wb.createSheet(workSheetNames[i]);
  42.  
  43.  
  44. Statement st = conn.createStatement();
  45. System.out.println("--------------------------");
  46. System.out.println(sqls[i]);
  47. ResultSet rs = st.executeQuery(sqls[i]);
  48. ResultSetMetaData rsMetaData = rs.getMetaData();
  49. int numberOfColumns = rsMetaData.getColumnCount();
  50. int rowCounter=0;
  51. HSSFRow rowHeader = sheet.createRow(rowCounter);
  52. for(int columnCounter=0; columnCounter<lt;numberOfColumns;columnCounter++){
  53.  
  54. rowHeader.createCell(columnCounter).setCellValue(rsMetaData.getColumnName(columnCounter+1));
  55. setFilledColorStyle(rowHeader.getCell(columnCounter),style2);
  56. }
  57. sheet.setAutoFilter(org.apache.poi.ss.util.CellRangeAddress.valueOf("A1:"+ (Character.toString((char)( 65+numberOfColumns-1)))+"1"));
  58.  
  59. rowCounter++;
  60. while(rs.next()) {
  61. // Create a row and put some cells in it. Rows are 0 based.
  62. HSSFRow row = sheet.createRow(rowCounter);
  63. for(int columnCounter=0; columnCounter<lt;numberOfColumns;columnCounter++){
  64. row.createCell(columnCounter).setCellValue(rs.getString(columnCounter+1));
  65. setThinBorderStyle(row.getCell(columnCounter),style1);
  66.  
  67. }
  68. rowCounter++;
  69. }
  70. rs.close();
  71. st.close();
  72. }
  73.  
  74.  
  75. // Write the output
  76.  
  77. wb.write(out);
  78. out.close();
  79. conn.close();
  80. System.out.println("Created Successfully");
  81. }
  82.  
  83. public static void main(String[] args) throws Exception{
  84. OutputStream out = new FileOutputStream("c:/admin.xls");
  85. createExcel(" select * from tab", out);
  86. }
  87. public static Connection getOracleConnection() throws Exception {
  88. String driver = "oracle.jdbc.driver.OracleDriver";
  89. String url = "jdbc:oracle:thin:@localhost:XE";
  90.  
  91.  
  92. String username = "system";
  93. String password = "password";
  94.  
  95.  
  96.  
  97.  
  98. Class.forName(driver); // load Oracle driver
  99. Connection conn = DriverManager.getConnection(url, username, password);
  100. return conn;
  101. }
  102. //This method set the thin border style
  103. private static void setThinBorderStyle(Cell cell, CellStyle style) {
  104. style.setBorderBottom(CellStyle.BORDER_THIN);
  105. style.setBottomBorderColor(IndexedColors.RED.getIndex());
  106. style.setBorderLeft(CellStyle.BORDER_THIN);
  107. style.setLeftBorderColor(IndexedColors.RED.getIndex());
  108. style.setBorderRight(CellStyle.BORDER_THIN);
  109. style.setRightBorderColor(IndexedColors.RED.getIndex());
  110. style.setBorderTop(CellStyle.BORDER_THIN);
  111. style.setTopBorderColor(IndexedColors.RED.getIndex());
  112. cell.setCellStyle(style);
  113. }
  114. //This method set the dashed border style
  115. private static void setDashedBorderStyle(Cell cell, CellStyle style) {
  116. style.setBorderBottom(CellStyle.BORDER_DASHED);
  117. style.setBottomBorderColor(IndexedColors.GREEN.getIndex());
  118. style.setBorderLeft(CellStyle.BORDER_DASHED);
  119. style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
  120. style.setBorderRight(CellStyle.BORDER_DASHED);
  121. style.setRightBorderColor(IndexedColors.GREEN.getIndex());
  122. style.setBorderTop(CellStyle.BORDER_DASHED);
  123. style.setTopBorderColor(IndexedColors.GREEN.getIndex());
  124. cell.setCellStyle(style);
  125. } // This method set the dotted border style
  126. private static void setFilledColorStyle(Cell cell, CellStyle style) {
  127. // style.setFillBackgroundColor(new HSSFColor.YELLOW().getIndex());
  128. style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
  129.  
  130. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
  131. // style.setBorderLeft(CellStyle.BORDER_DOTTED);
  132. // style.setLeftBorderColor(IndexedColors.BLUE.getIndex());
  133. // style.setBorderRight(CellStyle.BORDER_DOTTED);
  134. // style.setRightBorderColor(IndexedColors.BLUE.getIndex());
  135. // style.setBorderTop(CellStyle.BORDER_DOTTED);
  136. // style.setTopBorderColor(IndexedColors.BLUE.getIndex());
  137. cell.setCellStyle(style);
  138. }
  139.  
  140. }
  141.  
  142. sets Auto filter
  143.  
  144. sheet.setAutoFilter(CellRangeAddress.valueOf("A1:N1"));
  145.  
  146. String lastCellReference = "A1";
  147.  
  148. cell = row.createCell((short) columnCount);
  149. cell.setCellValue(rs.getInt(i));
  150. lastCellReference=cell.getReference();
  151.  
  152. sheet1.setAutoFilter(CellRangeAddress.valueOf("A1:"+ lastCellReference));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement