You are on page 1of 5

package rec;

import java.io.*; import java.sql.*; import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFDataFormatter; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFFontFormatting; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.util.HSSFColor;

public class CreateExcelFile{ public static void maina(ArrayList a){ try{ System.out.println(a); String s=(String) a.get(0); String s1=(String) a.get(1); String s2=(String) a.get(2);

String s3=(String) a.get(3); String s4=(String) a.get(4); String s5=(String) a.get(5); String s6=(String) a.get(6); String s7=(String) a.get(7); String filename="c:/data.xls" ; HSSFWorkbook hwb=new HSSFWorkbook(); HSSFSheet sheet = hwb.createSheet("new sheet");

HSSFRow rowhead= sheet.createRow((short)0);

HSSFCell cellA1 = rowhead.createCell((short) 0); cellA1.setCellValue(" 8 ");

HSSFCell cellB1 = rowhead.createCell((short) 1); cellB1.setCellValue(" 9 ");

HSSFCell cellC1 = rowhead.createCell((short) 2); cellC1.setCellValue(" 35 "); HSSFCell cellD1 = rowhead.createCell((short) 3); cellD1.setCellValue(" 49 "); HSSFCell cellE1 = rowhead.createCell((short) 4); cellE1.setCellValue(" 56 "); HSSFCell cellF1 = rowhead.createCell((short) 5); cellF1.setCellValue(" 34 "); HSSFCell cellG1 = rowhead.createCell((short) 6);

cellG1.setCellValue(" 52 "); HSSFCell cellH1 = rowhead.createCell((short) 7); cellH1.setCellValue(" 115 ");

HSSFCellStyle cellStyle = hwb.createCellStyle(); cellStyle.setFillForegroundColor(HSSFColor.GOLD.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

cellA1.setCellStyle(cellStyle); cellB1.setCellStyle(cellStyle); cellC1.setCellStyle(cellStyle); cellD1.setCellStyle(cellStyle); cellE1.setCellStyle(cellStyle); cellF1.setCellStyle(cellStyle); cellG1.setCellStyle(cellStyle); cellH1.setCellStyle(cellStyle);

//

Class.forName("com.mysql.jdbc.Driver");

// Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydemo", "root", ""); // Statement st=con.createStatement();

//

ResultSet rs=st.executeQuery("Select * from FIXExcedata"); int i=2; HSSFCellStyle cellStyle2 = hwb.createCellStyle(); cellStyle2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index); cellStyle2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

//

while(rs.next()){ HSSFRow rowhead2= sheet.createRow((short)i);

HSSFCell cellA2 = rowhead2.createCell((short) 0); cellA2.setCellValue(s); // cellA2.setCellValue(rs.getString("Symbol")); HSSFCell cellB2 = rowhead2.createCell((short) 1); cellB2.setCellValue(s1); HSSFCell cellC2 = rowhead2.createCell((short) 2); cellC2.setCellValue(s2); HSSFCell cellD2 = rowhead2.createCell((short) 3); cellD2.setCellValue(s3); HSSFCell cellE2 = rowhead2.createCell((short) 4); cellE2.setCellValue(s4); HSSFCell cellF2 = rowhead2.createCell((short) 5); cellF2.setCellValue(s5); HSSFCell cellG2 = rowhead2.createCell((short) 6); cellG2.setCellValue(s6); HSSFCell cellH2 = rowhead2.createCell((short) 7);

cellH2.setCellValue(s7); // if(i%2==0){ cellA2.setCellStyle(cellStyle2); cellB2.setCellStyle(cellStyle2); cellC2.setCellStyle(cellStyle2); cellD2.setCellStyle(cellStyle2); cellE2.setCellStyle(cellStyle2); cellF2.setCellStyle(cellStyle2); cellG2.setCellStyle(cellStyle2); cellH2.setCellStyle(cellStyle2); } i++; // } FileOutputStream fileOut = new FileOutputStream(filename); hwb.write(fileOut); fileOut.close(); System.out.println("Your excel file has been generated!");

} catch ( Exception ex ) { System.out.println(ex); } } }

You might also like