Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


Harshu
Excel
2 Answer(s)      4 years and 3 months ago
Posted in : JSP-Servlet


ViewOrder.jsp

<%@ page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCellStyle"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFFont"%>
<%@ page import="org.apache.poi.hssf.util.HSSFColor"%>
//<%@ page contentType="application/vnd.ms-excel" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>

<%


HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("View Order");
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)4);
HSSFRow row1 =sheet.createRow((short)3);
HSSFCell cell1 = row1.createCell((short)0);
HSSFCell cell2 = row1.createCell((short)1);
HSSFCell cell3 = row1.createCell((short)2);
HSSFCell cell4= row1.createCell((short)3);
HSSFCell cell5 = row1.createCell((short)4);
HSSFCell cell6 = row1.createCell((short)5);
HSSFCell cell7 = row1.createCell((short)6);
HSSFCell cell8 = row1.createCell((short)7);
HSSFCell cell9 = row1.createCell((short)8);
HSSFCell cell10 = row1.createCell((short)9);
HSSFCell cell11 = row1.createCell((short)10);
HSSFCell cell12 = row1.createCell((short)11);
HSSFCell cell13 = row1.createCell((short)12);
HSSFCell cell14 = row1.createCell((short)13);

HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)14);
font.setFontName("Arial");
font.setItalic(true);
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);

HSSFFont font1 = wb.createFont();
font1.setFontHeightInPoints((short)12);
font1.setFontName("Arial");
font1.setUnderline((byte)3);
HSSFCellStyle cellStyle1 = wb.createCellStyle();
cellStyle1.setFont(font1);
cellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell1.setCellStyle(cellStyle1);

HSSFFont font2 = wb.createFont();
font2.setFontHeightInPoints((short)12);
font2.setFontName("Arial");
HSSFCellStyle cellStyle2 = wb.createCellStyle();
cellStyle2.setFont(font2);
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell2.setCellStyle(cellStyle2);

HSSFFont font3 = wb.createFont();
font3.setFontHeightInPoints((short)12);
font3.setFontName("Arial");
HSSFCellStyle cellStyle3 = wb.createCellStyle();
cellStyle3.setFont(font3);
cellStyle3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell3.setCellStyle(cellStyle3);

HSSFFont font4 = wb.createFont();
font4.setFontHeightInPoints((short)12);
font4.setFontName("Arial");
HSSFCellStyle cellStyle4 = wb.createCellStyle();
cellStyle4.setFont(font4);
cellStyle4.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell4.setCellStyle(cellStyle4);

HSSFFont font5 = wb.createFont();
font5.setFontHeightInPoints((short)12);
font5.setFontName("Arial");
HSSFCellStyle cellStyle5 = wb.createCellStyle();
cellStyle5.setFont(font5);
cellStyle5.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell5.setCellStyle(cellStyle5);

HSSFFont font6 = wb.createFont();
font6.setFontHeightInPoints((short)12);
font6.setFontName("Arial");
HSSFCellStyle cellStyle6 = wb.createCellStyle();
cellStyle6.setFont(font6);
cellStyle6.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell6.setCellStyle(cellStyle6);

HSSFFont font7 = wb.createFont();
font7.setFontHeightInPoints((short)12);
font7.setFontName("Arial");
HSSFCellStyle cellStyle7 = wb.createCellStyle();
cellStyle7.setFont(font7);
cellStyle7.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell7.setCellStyle(cellStyle7);

HSSFFont font8 = wb.createFont();
font8.setFontHeightInPoints((short)12);
font8.setFontName("Arial");
HSSFCellStyle cellStyle8 = wb.createCellStyle();
cellStyle8.setFont(font8);
cellStyle8.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell8.setCellStyle(cellStyle8);

HSSFFont font9 = wb.createFont();
font9.setFontHeightInPoints((short)12);
font9.setFontName("Arial");
HSSFCellStyle cellStyle9 = wb.createCellStyle();
cellStyle9.setFont(font9);
cellStyle9.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell9.setCellStyle(cellStyle9);

HSSFFont font10 = wb.createFont();
font10.setFontHeightInPoints((short)12);
font10.setFontName("Arial");
HSSFCellStyle cellStyle10 = wb.createCellStyle();
cellStyle10.setFont(font10);
cellStyle10.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell10.setCellStyle(cellStyle10);

HSSFFont font11 = wb.createFont();
font11.setFontHeightInPoints((short)12);
font11.setFontName("Arial");
HSSFCellStyle cellStyle11 = wb.createCellStyle();
cellStyle11.setFont(font11);
cellStyle11.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell11.setCellStyle(cellStyle11);

HSSFFont font12 = wb.createFont();
font12.setFontHeightInPoints((short)12);
font12.setFontName("Arial");
HSSFCellStyle cellStyle12 = wb.createCellStyle();
cellStyle12.setFont(font12);
cellStyle12.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell12.setCellStyle(cellStyle12);

/*HSSFFont font13 = wb.createFont();
font13.setFontHeightInPoints((short)12);
font13.setFontName("Arial");
HSSFCellStyle cellStyle13 = wb.createCellStyle();
cellStyle13.setFont(font13);
cellStyle13.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell13.setCellStyle(cellStyle13);

HSSFFont font14 = wb.createFont();
font14.setFontHeightInPoints((short)12);
font14.setFontName("Arial");
HSSFCellStyle cellStyle14 = wb.createCellStyle();
cellStyle14.setFont(font14);
cellStyle14.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell14.setCellStyle(cellStyle14);*/

Connection con=null;
Statement stmt=null;
ResultSet rs=null;
int i=Integer.parseInt(request.getParameter("Num"));


//catch ( Exception ex ){}
%>

<%
try {


Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Advdsn");
stmt=con.createStatement();

rs=stmt.executeQuery("SELECT OrderTab.OrderNo, OrderTab.Day, OrderTab.Name, ClassifiedText.State, ClassifiedText.City, ClassifiedText.MainCategory, ClassifiedText.SubCategory, ClassifiedText.NewsPaper, ClassifiedText.Size ,ClassifiedText.Page ,ClassifiedText.Total , ClassifiedText.Day &'/' & ClassifiedText.Month & '/' & ClassifiedText.Year AS DateofAd FROM OrderTab INNER JOIN ClassifiedText ON OrderTab.OrderNo=ClassifiedText.OrderNo WHERE (((OrderTab.OrderNo)="+i+" And (OrderTab.OrderNo)=[ClassifiedText].[OrderNo]))");
//rs=stmt.executeQuery("select OrderNo,Name,Day from OrderTab where OrderNo="+i+"");


cell.setCellValue("Order No wise Report for Classified Text Ads");
cell1.setCellValue("OrderNo");
cell2.setCellValue("Day");
cell3.setCellValue("Name");
cell4.setCellValue("AdvtDate");
cell5.setCellValue("State");
cell6.setCellValue("City");
cell7.setCellValue("MainCat");
cell8.setCellValue("SubCat");
cell9.setCellValue("Newspaper");
//cell10.setCellValue("ImageName");
cell10.setCellValue("Size");
//cell12.setCellValue("Color");
cell11.setCellValue("AdPages");
cell12.setCellValue("Total");

while(rs.next())

{
HSSFRow row2 =sheet.createRow((short)4);
row2.createCell((short)0).setCellValue(rs.getInt("OrderNo"));
row2.createCell((short)1).setCellValue(rs.getString("Day"));
row2.createCell((short)2).setCellValue(rs.getString("Name"));
row2.createCell((short)3).setCellValue(rs.getString("DateOfAd"));
row2.createCell((short)4).setCellValue(rs.getString("State"));
row2.createCell((short)5).setCellValue(rs.getString("City"));
row2.createCell((short)6).setCellValue(rs.getString("MainCategory"));
row2.createCell((short)7).setCellValue(rs.getString("SubCategory"));
row2.createCell((short)8).setCellValue(rs.getString("Newspaper"));
//row2.createCell((short)9).setCellValue(rs.getInt("ImageName"));
row2.createCell((short)9).setCellValue(rs.getString("Size"));
//row2.createCell((short)11).setCellValue(rs.getString("Color"));
row2.createCell((short)10).setCellValue(rs.getString("Page"));
row2.createCell((short)11).setCellValue(rs.getInt("Total"));

}

FileOutputStream fileOut = new FileOutputStream("C:\\excel\\ViewOrd"+i+".xls");
wb.write(fileOut);
fileOut.close();
out.println("Your excel file has been generated");
}
catch(Exception e2)
{
out.println(e2.toString());
}
//}



%>

Hello Sir ,

In this code my fields name are very long and it does not fit the excel cell so i want to increase the width of the column from the code i tried a lot but not working ,and i too want to set the font to bold i wrote font.setBold(true);
then its giving me error.

How to solve this error and when i write ALIGN_CENTER using HSSFCellStyle then too its giving me error??

And i also wanted to make the Border around the cell i wrote the code for same but not making the change in the output so please help how to solve this???

Please reply me sir as soon as possible??

Thank You Sir.








Advertisement
View Answers

May 13, 2010 at 11:29 AM


Hi Friend,

You need to download and set the path of poi-3.6-20091214.jar.
We have modified your code:

<%@ page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@ page import="org.apache.poi.hssf.usermodel.*"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCellStyle"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFFont"%>
<%@ page import="org.apache.poi.hssf.util.HSSFColor"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("View Order");
HSSFRow row = sheet.createRow((short)0);
row.createCell((short) 0).setCellValue("OrderNo");
row.createCell((short) 1).setCellValue("Day");
row.createCell((short) 2).setCellValue("Name");
row.createCell((short) 3).setCellValue("AdvtDate");
row.createCell((short) 4).setCellValue("State");
row.createCell((short) 5).setCellValue("City");
row.createCell((short) 6).setCellValue("MainCat");
row.createCell((short) 7).setCellValue("SubCat");
row.createCell((short) 8).setCellValue("Newspaper");
row.createCell((short) 9).setCellValue("ImageName");
row.createCell((short) 10).setCellValue("Size");
row.createCell((short) 11).setCellValue("Color");
row.createCell((short) 12).setCellValue("AdPages");
row.createCell((short) 13).setCellValue("Total");

for(short i=0;i<14;i++){
HSSFCell cell=row.getCell(i) ;

HSSFFont font = wb.createFont();
HSSFCellStyle cellStyle = wb.createCellStyle();
font.setFontHeightInPoints((short)14);
font.setFontName("Arial Black");
font.setItalic(true);
cell.setCellStyle(cellStyle);
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THICK);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THICK);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THICK);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THICK) ;
sheet.autoSizeColumn(i);
}
%>


May 13, 2010 at 11:30 AM


continue..

<%
int i=Integer.parseInt(request.getParameter("Num"));
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:access");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT OrderTab.OrderNo, OrderTab.Day, OrderTab.Name, ClassifiedText.State, ClassifiedText.City, ClassifiedText.MainCategory, ClassifiedText.imageName, ClassifiedText.SubCategory, ClassifiedText.NewsPaper, ClassifiedText.Size , ClassifiedText.Color , ClassifiedText.Page , ClassifiedText.Total , ClassifiedText.Day &'/' & ClassifiedText.Month & '/' & ClassifiedText.Year AS DateofAd FROM OrderTab INNER JOIN ClassifiedText ON OrderTab.OrderNo= ClassifiedText.OrderNo WHERE ((OrderTab.OrderNo)="+i+" And (OrderTab.OrderNo)=[ ClassifiedText].[OrderNo])");
HSSFRow row2 =sheet.createRow((short)2);
while(rs.next()){
row2.createCell((short)0).setCellValue(rs.getInt("OrderNo"));
row2.createCell((short)1).setCellValue(rs.getString("Day"));
row2.createCell((short)2).setCellValue(rs.getString("Name"));
row2.createCell((short)3).setCellValue(rs.getString("DateOfAd"));
row2.createCell((short)4).setCellValue(rs.getString("State"));
row2.createCell((short)5).setCellValue(rs.getString("City"));
row2.createCell((short)6).setCellValue(rs.getString("MainCategory"));
row2.createCell((short)7).setCellValue(rs.getString("SubCategory"));
row2.createCell((short)8).setCellValue(rs.getString("Newspaper"));
row2.createCell((short)9).setCellValue(rs.getString("imageName"));
row2.createCell((short)10).setCellValue(rs.getString("Size"));
row2.createCell((short)11).setCellValue(rs.getString("Color"));
row2.createCell((short)12).setCellValue(rs.getString("Page"));
row2.createCell((short)13).setCellValue(rs.getInt("Total"));
}
File f = new File ("C:\\sf.xls");
FileOutputStream fileOut = new FileOutputStream(f);
wb.write(fileOut);
fileOut.close();
String filename=f.getName();
response.setContentType ("application/ms-excel");
response.setHeader ("Content-Disposition", "attachment; filename=\""+filename+"\"");

String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();

int bit = 256;
try {
while ((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();

}
catch(Exception e2){
out.println(e2.toString());
}
%>

Thanks


Related Tutorials/Questions & Answers:
excel
excel  how to read columns values of excel file in java and store in array list in java like col1 col2 2 3 1 7 7 9 then list1 have values of col1 and list2 have values of col2...  
excel
excel  how to save excel sheet data into the database using poi api?   import java.io.*; import java.sql.*; import java.util.*; import java.util.regex.*; import org.apache.poi.hssf.usermodel.
Advertisements
JDBC to EXcel
JDBC to EXcel  How to insert data in excel sheet from java
JAVA EXCEL
JAVA EXCEL  How to read the contents of an excel, perform some calculation and wrote the calculated values to another excel using poi
Excel to database
Excel to database  Hi I want to update SQL database with the values from excel using java code. Can you help me
download excel
download excel  hi i create an excel file but i don't i know how to give download link to that excel file please give me any code or steps to give download link
Sql to excel
Sql to excel  How to convert a Sql Data into excel Doc with Using Netbeans
JAVA_EXCEL
JAVA_EXCEL  I need to read the all the contents of excel which has... content another excel. For example if I have three tabs in an excel with some contents... to another excel. Please help me out with this problem
JSP TO EXCEL
JSP TO EXCEL  Hi sir/mam, How to import data to excel using jsp without retrieving database.   friend, you can't import excel data into the middle of an HTML pages (your JSP will result in an HTML page
To Generate Excel
With EXCEL in it has to open up how can i achieve this pls explain
convert excel into pdf
convert excel into pdf  How to convert excel into pdf files
uploading and reading the excel daa
uploading and reading the excel daa  uploading the excel file not getting the data of the excel
excel to database
excel to database   How to export data from excel sheet to mysql database by using java with request parameter Here q=40 is a request parameter ..request parameter using i export the all data to database   <
How to Unlock Excel Password
How to Unlock Excel Password  anyone help me how to recover lost Excel file password
struts2 excel downloads
struts2 excel downloads  hi friend, how to set Timestamps(Date+time) values to excel sheet cells
Excel Column change
Excel Column change  How to change sequence of columns in Excel sheet through Java
VB and excel - Development process
VB and excel  Hello sir, I want to create an excel sheet in VB 6.0 and get the data from Excel to MS-Access and set the macro in MS-Access for the Excel sheet data. Data contain in Excel Sheet to display in MS-Access and set
JSP to Excel - JSP-Servlet
JSP to Excel  Need an example of JSP and Excel database connection. Thanks
how to use Excel Templet to write excel file using java.
how to use Excel Templet to write excel file using java.  how to use Excel Templet to write excel file using java
struts2 excel downloads
struts2 excel downloads  hi sir, am new to the real time environment. am going to create excel sheet and setting values to excel sheet from db table dynamically by using Hibernate persistence logic.......am using struts2
jsp to excel - JSP-Servlet
jsp to excel  Hi All, I'm writing a program of jsp to Excel conversion with Using POI. My problem is i'm unable to put multiple data into excel...)); } FileOutputStream fileOut = new FileOutputStream("c:\\excel\\wct.xls
Excel Password Cracker Software
Excel Password Cracker Software  Hello I just want to know what is Excel Password Recovery software? I want need to recover a worksheet since I forgotten
Export html data to excel
Export html data to excel  I am trying to export data from an html page to an excel sheet.Any advise
currency symbols in excel
currency symbols in excel  How to add a currency symbol in Excel sheet? Please guide me
excel uploading in jsp
excel uploading in jsp  could you provide the source code for: 1)have to upload an empty excel sheet at client side i.e if client clicks an excel icon an empty excel sheet should open 2)when they fill data in that sheet and click
connect jdbc to an Excel spreadsheet
connect jdbc to an Excel spreadsheet  hello, How can I connect to an Excel spreadsheet file using jdbc?   Hello Friend, Follow... Excel Driver(*.xls) Select work book or excel file and Create the DSN name (e.g
how to use Excel Template to write excel file using java
how to use Excel Template to write excel file using java  How to use Excel template to write data in that file using java
Reading an excel file into array
Reading an excel file into array  Hi, I'm trying to read in an excel file, search a column for containing key words (entered by a user) and then displaying the matching rows in a table. I'm fairly new to JavaScript. Can anyone
excel to ms access
excel to ms access  hello sir i am developing a application which requires extracting data from excel sheet and place in the ms access database using java programming for example: in excel sheet i have 2 columns and more than
Excel - JSP-Servlet
Excel  How to export data from jsp to excel sheet. I am using struts1.2 in my application.  Hi friend, Code to data from Jsp to excel...: "success.jsp" For more information on excel sheet Using JSP visit
search for exact match in excel
search for exact match in excel  How to search for an exact match in excel and show data if the matched sting is found? Please tell me the script. Thanks in Advance
How to export grid into excel
How to export grid into excel  Hi, i created a grid panel i have to export it to the excel. please help me by some sample code. thanks in advance. cool day dude
search for exact match in excel
search for exact match in excel  How to search for an exact match in excel and show data if the matched sting is found? Please tell me the script. Thanks in Advance
save data in excel
save data in excel  hello sir, i'm new to this field i want to know can i save data in excel by html forms input,means if any one fill up form online then it is save offline in excel how can i do it can you give me solution sir
save data in excel
save data in excel  hello sir, i'm new to this field i want to know can i save data in excel by html forms input,means if any one fill up form online then it is save offline in excel how can i do it can you give me solution sir
odbc MS EXCEL
odbc MS EXCEL  hey my whole database is stored in MS Excel. so please tell me that can we connect to that database. if yes, how to do that. please help me out i am doing it in netbeans can please tell me the steps
export to excel - JSP-Servlet
export to excel  Hi this is priti. I need a help. What is my question is i have a report page here i need a button cllaed "Export to excel". After clicking the button all the values from the jsp page will save to a excel file
insert excel sheet into mysql as a table
insert excel sheet into mysql as a table  sir, i want to import an excel sheet into mysql5.0 database as in the table format using tomcat 6.0 by jsp
convert html to excel using jsp
convert html to excel using jsp   i want to convert a html page into mcrosoft excel page using jsp.how i do
read data from Excel sheet
read data from Excel sheet  Hi, How to read data from an excel sheet using JSP. Thank you
jsp excel code - JSP-Servlet
jsp excel code  hi how to store html form data into excel sheet by using jsp? and repeat this process continuously for entire sheet
Excel file Handling in Java
Excel file Handling in Java  Hello Sir, I am new to Java, I have Started Java Core by myself. I want to make a project which include 3-4 Excel file to handle.Please explain how to read and write Excel file in Java.Is it tough
reading excel sheet in java
reading excel sheet in java  can anyone tell me a proper java code to read excel sheet using poi   Here is a java code that reads an excel file using POI api and display the data on the console. import java.io.
ODCB-EXCEL - Java Beginners
ODCB-EXCEL  Hi, i would like to extract data from an excel file. my excel file name is: TEST.xls the worksheet name is : qas i have configure...: "java.sql.SQLException: [Microsoft][Pilote ODBC Excel] Not enough parameters 1
importing excel file
importing excel file  Hi All I am developing a java application whre we upload a excel file in to database and all excel value should import... it is not showing real excel sheet value. please suggest good way to solve this problem
update excel sheet using jsp::
update excel sheet using jsp::   Hi Sir,... I have a excel... given excel sheet and display it into another excel sheet using jsp... page to delete the data in excel but it not working.. it shows null point
How to insert rows from Excel spreadsheet into database by browsing the excel file?
How to insert rows from Excel spreadsheet into database by browsing the excel file?  I want to insert rows from excel sheet to database.for this i... the excel file using file browsing dialogue through form in JSP. How can i select
get values from Excel to database
get values from Excel to database   hi i want to insert values from Excel file into database.Whatever field and contents are there in excel file that should go to database which exists. am using SQL Server management studio
To read a excel with chart - Java Beginners
To read a excel with chart   Hi, I need to read the data in an excel which is in chart format using java. when I directly change the extention of excel file to CSV i am not getting the data in the chart.Please help me
Excel conversion tool. - Java Beginners
Excel conversion tool.  Hi, I need a conversion tool which can convert .xls(Excel 2003) to .xlsx (Excel 2007) and vice-versa. Please suggest any links ro tools. Thank You
Advertisements
 

 

 

DMCA.com