Below is the code which demonstrates the generation of multiple excel sheets in single excel file using java.
Also it demonstrates the way to connect to mysql & oracle through java
//CODE:
package com.MyReports;
import java.sql.*;
public class sqlserver_con {
//SQL SERVER CONNECTION
Connection sqlcon = null;
public sqlserver_con () {
try{
//MYSQL LOGIN CREDENTIALS
String url = "jdbc:mysql://192.100.101.1:3306/";
String dbName = "mysqldb";
String driver = "com.mysql.jdbc.Driver";
String userName = "username";
String password = "password";
//END
}catch(Exception e) {
System.out.println("ERROR:"+ e);
}
}
public void getData() {
ResultSet rs=null;
try{
//EXCEL GENERATION COMMONS
HSSFWorkbook hwb = new HSSFWorkbook();
// CreationHelper createHelper = hwb.getCreationHelper();
CellStyle style = hwb.createCellStyle();
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
//END
//EXCEL SHEET 0
Sheet sheet = hwb.createSheet("SHEET1");
Row rowhead = sheet.createRow((short)0);
Row row1 = sheet.createRow((short)0);
row1.createCell( 0).setCellValue("TEXT");
Row row2 = sheet.createRow((short)2);
//EXCEL SHEET 0 END
//EXCEL SHEET 1
Sheet sheet1 = hwb.createSheet("SHEET2");
Row rowhead1 = sheet1.createRow((short)0);
Row row1_1 = sheet1.createRow((short)0);
row1_1.createCell( 0).setCellValue("TEXT");
Row row2_1 = sheet1.createRow((short)2);
//EXCEL SHEET 1 END
int j=0;
int newline1=3; //Setting row & column for data
//Executing mysql query to retrieve D
String query = "SELECT "+
" CASE "+
" WHEN DATE_FORMAT(ost.created, '%k') > 22 OR DATE_FORMAT(ost.created, '%k') < 9 THEN '22 ~ 9' "+
" WHEN DATE_FORMAT(ost.created, '%k') >= 9 AND DATE_FORMAT(ost.created, '%k') < 11 THEN '9 ~ 11' "+
" ELSE DATE_FORMAT(ost.created, '%k') "+
" END time_slot, "+
" DATE_FORMAT(ost.created, '%d-%m-%Y %k:%i') ost_created "+
" FROM myTABLE "+
" JOIN dept osd "+
" ON osd.dept_id = ost.dept_id "+
// " WHERE ost.created > DATE_SUB(curdate(), INTERVAL 1 DAY)"; //YESTERDAY DATE
" WHERE ost.created > DATE_FORMAT(NOW(), '%Y-%m-%d')";
Class.forName(driver).newInstance();
mysqlcon = DriverManager.getConnection(url+dbName,userName,password);
Statement stmt = mysqlcon.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs_or.next())
{
j++;
newline1++;
Row row = sheet1.createRow((short)newline1);
row.createCell(0).setCellValue(rs_or.getString("time_slot"));
row.createCell(1).setCellValue(rs_or.getString("TEXT"));
row.createCell((short) 2).setCellValue("");
}
//MYSQL QUERY END
stmt.close();
rs_or.close();
//**********************************************SHEET 2 ************************
int k=0;
int newline2=3; //Setting row & column for data
CallableStatement stmt_or=con.prepareCall("{ call PKG.GET_DATA(?) }");
stmt.registerOutParameter(1, OracleTypes.CURSOR);
stmt.execute();
rs_or= (ResultSet)stmt.getObject(1);
while(rs_or.next())
{
k++;
newline2++;
Row row = sheet2.createRow((short)newline2);
row.createCell(0).setCellValue(rs_or.getString("time_slot"));//time_slot
row.createCell(1).setCellValue(rs_or.getString("TEXT"));
row.createCell((short) 2).setCellValue("");
}
//ORACLE PROC END
stmt.close();
rs_or.close();
//GENERATING EXCEL
System.out.println("Generating excel");
FileOutputStream out =new FileOutputStream(new File(FOLDER.class.getProtectionDomain
().getCodeSource().getLocation().getPath()+"/MYFLDER/mydata.xls"));
hwb.write(out);
out.close();
//END
}
catch(Exception e1) {
//
}
}
public static void main(String[] args) {
sqlserver_con ee = new sqlserver_con ();
ee.getData();
ee.closeConnection();
}
public void closeConnection() {
try{
sqlcon.close();
System.out.println("closing SQLSERVER DB connection");
}catch(Exception e) {
System.out.println("ERROR: "+e);
}
}
Also it demonstrates the way to connect to mysql & oracle through java
//CODE:
package com.MyReports;
import java.sql.*;
public class sqlserver_con {
//SQL SERVER CONNECTION
Connection sqlcon = null;
public sqlserver_con () {
try{
//MYSQL LOGIN CREDENTIALS
String url = "jdbc:mysql://192.100.101.1:3306/";
String dbName = "mysqldb";
String driver = "com.mysql.jdbc.Driver";
String userName = "username";
String password = "password";
//END
}catch(Exception e) {
System.out.println("ERROR:"+ e);
}
}
public void getData() {
ResultSet rs=null;
try{
//EXCEL GENERATION COMMONS
HSSFWorkbook hwb = new HSSFWorkbook();
// CreationHelper createHelper = hwb.getCreationHelper();
CellStyle style = hwb.createCellStyle();
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
//END
//EXCEL SHEET 0
Sheet sheet = hwb.createSheet("SHEET1");
Row rowhead = sheet.createRow((short)0);
Row row1 = sheet.createRow((short)0);
row1.createCell( 0).setCellValue("TEXT");
Row row2 = sheet.createRow((short)2);
//EXCEL SHEET 0 END
//EXCEL SHEET 1
Sheet sheet1 = hwb.createSheet("SHEET2");
Row rowhead1 = sheet1.createRow((short)0);
Row row1_1 = sheet1.createRow((short)0);
row1_1.createCell( 0).setCellValue("TEXT");
Row row2_1 = sheet1.createRow((short)2);
//EXCEL SHEET 1 END
int j=0;
int newline1=3; //Setting row & column for data
//Executing mysql query to retrieve D
String query = "SELECT "+
" CASE "+
" WHEN DATE_FORMAT(ost.created, '%k') > 22 OR DATE_FORMAT(ost.created, '%k') < 9 THEN '22 ~ 9' "+
" WHEN DATE_FORMAT(ost.created, '%k') >= 9 AND DATE_FORMAT(ost.created, '%k') < 11 THEN '9 ~ 11' "+
" ELSE DATE_FORMAT(ost.created, '%k') "+
" END time_slot, "+
" DATE_FORMAT(ost.created, '%d-%m-%Y %k:%i') ost_created "+
" FROM myTABLE "+
" JOIN dept osd "+
" ON osd.dept_id = ost.dept_id "+
// " WHERE ost.created > DATE_SUB(curdate(), INTERVAL 1 DAY)"; //YESTERDAY DATE
" WHERE ost.created > DATE_FORMAT(NOW(), '%Y-%m-%d')";
Class.forName(driver).newInstance();
mysqlcon = DriverManager.getConnection(url+dbName,userName,password);
Statement stmt = mysqlcon.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs_or.next())
{
j++;
newline1++;
Row row = sheet1.createRow((short)newline1);
row.createCell(0).setCellValue(rs_or.getString("time_slot"));
row.createCell(1).setCellValue(rs_or.getString("TEXT"));
row.createCell((short) 2).setCellValue("");
}
//MYSQL QUERY END
stmt.close();
rs_or.close();
//**********************************************SHEET 2 ************************
int k=0;
int newline2=3; //Setting row & column for data
CallableStatement stmt_or=con.prepareCall("{ call PKG.GET_DATA(?) }");
stmt.registerOutParameter(1, OracleTypes.CURSOR);
stmt.execute();
rs_or= (ResultSet)stmt.getObject(1);
while(rs_or.next())
{
k++;
newline2++;
Row row = sheet2.createRow((short)newline2);
row.createCell(0).setCellValue(rs_or.getString("time_slot"));//time_slot
row.createCell(1).setCellValue(rs_or.getString("TEXT"));
row.createCell((short) 2).setCellValue("");
}
//ORACLE PROC END
stmt.close();
rs_or.close();
//GENERATING EXCEL
System.out.println("Generating excel");
FileOutputStream out =new FileOutputStream(new File(FOLDER.class.getProtectionDomain
().getCodeSource().getLocation().getPath()+"/MYFLDER/mydata.xls"));
hwb.write(out);
out.close();
//END
}
catch(Exception e1) {
//
}
}
public static void main(String[] args) {
sqlserver_con ee = new sqlserver_con ();
ee.getData();
ee.closeConnection();
}
public void closeConnection() {
try{
sqlcon.close();
System.out.println("closing SQLSERVER DB connection");
}catch(Exception e) {
System.out.println("ERROR: "+e);
}
}
No comments:
Post a Comment