Thu Oct 23, 2008 3:58 pm
<%@ page import="java.util.*, java.util.ArrayList, java.text.*"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFPatriarch"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFClientAnchor"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFComment"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFRichTextString"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFPicture"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCellStyle "%>
<%@ page import="org.apache.poi.hssf.util.HSSFColor"%>
<%@ page import="java.io.ByteArrayOutputStream" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.File"%>
<%@ page import="java.io.InputStream"%>
<%@ page import="java.io.BufferedInputStream"%>
<%@ page import="com.jxcell.View"%>
<%@ page import="com.jxcell.CellException"%>
<%@ page import="com.jxcell.designer.Designer"%>
<%@ page import="java.io.IOException"%>
<%@ page
import="java.io.IOException,java.io.ByteArrayOutputStream,java.io.File
NotFoundException,java.io.OutputStream"%>
<%@ page import="org.apache.poi.poifs.filesystem.POIFSFileSystem "%>
<%
String blankExcelPath = "blank_excel.xls";
POIFSFileSystem fs = null;
try{
fs = new POIFSFileSystem(new FileInputStream
("D:/Tomcat 4.1/webapps/ExcelApp/blank_excel.xls"));
}
catch(FileNotFoundException fnfExc){
response.setContentType("text/html");
out.print("System Error occured.\nThe Blank Template
Excel is not present.");
}
catch(IOException ioExc){
out.print("System Error occured.\nIO related problem
with the template excel file.");
}
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos);
baos.close();
byte[] bytArr = (byte[]) baos.toByteArray();
String fileName = "1.xls";// + new Date().getTime() + ".xls";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-disposition", "attachment;
filename="+fileName);
File file = new File("D://Tomcat
4.1//webapps//ExcelApp//img//1.png");
long lengthh = file.length();
byte[] picData = new byte[(int)file.length()];
byte[] bytes = new byte[(int)file.length()];
InputStream input = new BufferedInputStream(new FileInputStream
(file));
FileInputStream picIn = new FileInputStream( file );
picIn.read( picData );
try {
int offset = 0;
int read = -1;
while((read = input.read()) != -1)
bytes[offset++] = (byte)read;
} finally {
input.close();
}
int indx = wb.addPicture(picData,HSSFWorkbook.PICTURE_TYPE_PNG);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor;
anchor=new HSSFClientAnchor(0,0,0,255,(short)0,5,(short)0,8);
anchor.setAnchorType(2);
patriarch.createPicture(anchor, indx);
Thu Oct 23, 2008 4:00 pm
import org.apache.poi.hssf.usermodel.*;
import java.io.*;
public class MyWorkBook
{
public static void main(String[] args)
{
int col=1,row=1;
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet testsheet=wb.createSheet("test");
System.out.println("The work book is created");
try
{
FileOutputStream fos=new FileOutputStream("sample.xls");
System.out.println("File sample.xls is created");
FileInputStream fis=new FileInputStream("home.jpg");
ByteArrayOutputStream img_bytes=new ByteArrayOutputStream();
int b;
while((b=fis.read())!=-1)
img_bytes.write(b);
fis.close();
HSSFClientAnchor anchor = new
HSSFClientAnchor(0,0,0,0,(short)col,row,(short)++col,++row);
int
index=wb.addPicture(img_bytes.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG);
HSSFSheet sheet=wb.getSheet("test");
HSSFPatriarch patriarch=sheet.createDrawingPatriarch();
patriarch.createPicture(anchor,index);
anchor.setAnchorType(2);
wb.write(fos);
System.out.println("Writing data to the xls file");
fos.close();
System.out.println("File closed");
}
catch(IOException ioe)
{
System.out.println("Hi ! You got an exception. "+ioe.getMessage());
}
}
}//end of class MyWorkBook
Sun May 13, 2012 2:48 pm
Mon May 14, 2012 12:30 pm
org.apache.commons.codec
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.