Total members 11759 |It is currently Thu Dec 13, 2018 7:19 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





I have tried to insert image to Excel Sheet, code is not working,
please can u check below code what is the problem.
Code:
<%@ 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);





Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

I havent seen your code but I have written a code which inserts an image into
the excel sheet using POI. Below is the code which I have written. This may help
you.

Code:
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


_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time

Hi,

I tried yor code to export image in excel on Android. I got an error. Copying logcat... please help!!

05-13 19:11:47.665: E/AndroidRuntime(7288): FATAL EXCEPTION: main
05-13 19:11:47.665: E/AndroidRuntime(7288): java.lang.NoClassDefFoundError: org.apache.commons.codec.digest.DigestUtils
05-13 19:11:47.665: E/AndroidRuntime(7288): at org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture(HSSFWorkbook.java:1580)
05-13 19:11:47.665: E/AndroidRuntime(7288): at com.android.testexcel.MyWorkBook.writeImageToExcel(MyWorkBook.java:30)
05-13 19:11:47.665: E/AndroidRuntime(7288): at com.android.testexcel.MainActivity.onClick(MainActivity.java:71)
05-13 19:11:47.665: E/AndroidRuntime(7288): at android.view.View.performClick(View.java:2408)
05-13 19:11:47.665: E/AndroidRuntime(7288): at android.view.View$PerformClick.run(View.java:8816)
05-13 19:11:47.665: E/AndroidRuntime(7288): at android.os.Handler.handleCallback(Handler.java:587)
05-13 19:11:47.665: E/AndroidRuntime(7288): at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 19:11:47.665: E/AndroidRuntime(7288): at android.os.Looper.loop(Looper.java:123)
05-13 19:11:47.665: E/AndroidRuntime(7288): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 19:11:47.665: E/AndroidRuntime(7288): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 19:11:47.665: E/AndroidRuntime(7288): at java.lang.reflect.Method.invoke(Method.java:521)
05-13 19:11:47.665: E/AndroidRuntime(7288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 19:11:47.665: E/AndroidRuntime(7288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 19:11:47.665: E/AndroidRuntime(7288): at dalvik.system.NativeStart.main(Native Method)


Thanks in Advance,
Hemant


Author:

does package below > exists on your android ? i think now , the code above was tested on desktop and web !!
Code:
org.apache.commons.codec



Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 4 posts ] 

  Related Posts  to : Image Insert in Excel File Using POI
 insert information in html file with java     -  
 Save Image to a file in ITK     -  
 Read an image bitmap (.bmp) file and draw it as texture     -  
 Change Image File Size (Height And Width) While Uploading     -  
 regarding to excel sheet     -  
 insert query example     -  
 how to connect applet with excel     -  
 VBA / Excel programmer needed     -  
 Insert using native query     -  
 convert word, Excel to PDF     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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