470,831 Members | 1,663 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,831 developers. It's quick & easy.

Grabbing graphic from excel file and display it as thumbnails

Dear all,

Currently, I am working on a project to make a web-based application using ASP 2.0 and C#. This application will ask user to input for an excel file which has graphs in it. Then the application will grab the graph as image file, and this image will be displayed as thumbnail in the page.

Is it possible to accomplish this feature?
If it is possible, would you please tell me how?
And if it is not possible, why?
Jan 31 '08 #1
1 1893
Finally after a very long experimento, my friend and I were succeed and we made it..This is how we make it..
{Well actually, we can't really grab the image of graphics of the excel files, but what we can really do is to convert the excel file into html file - which have graphics of excel as images in html file - and then wholla...we grab the images, and delete the html file :)

First, we need to use this classes
Expand|Select|Wrap|Line Numbers
  1. using Microsoft.Office.Interop.Excel;
  2. using System.Xml;
  3. using System.IO;
  4. using System.Threading;
And then..create an object to keep the excel file, this object will be an instance of class ApplicationClass.
Expand|Select|Wrap|Line Numbers
  1. ApplicationClass objExcel = new ApplicationClass();
Define the list of string to keep path of images file.
Expand|Select|Wrap|Line Numbers
  1. List<string> imageCollection = new List<string>();
Check if the access to the file is allowed, and the file size (in bytes) is bigger than 0, then the file can be processed.
Expand|Select|Wrap|Line Numbers
  1. if ((excelZipUpload.PostedFile != null) && (excelZipUpload.PostedFile.ContentLength > 0))
  2.         {
Next, I define the GUID. Why did I use this GUID? WEll, one excel file may contain several images of graphics. This graphic images must be collected in one folder. To make sure that the folder name is uniques then I use GUID as the name of the folder.This folder name will have the same GUID as the file excel has.
Expand|Select|Wrap|Line Numbers
  1. string guidFileName = Guid.NewGuid().ToString();
Prepare the session and other variables to keep the values of new excel file name, html file name, path of folder, and the xml generated from
string strFileName = System.IO.Path.GetFileName(excelZipUpload.PostedFi le.FileName);
bSucceed = true;
Session["ExcelFilePath"] = "ExcelFile/" + guidFileName + ".xls"; ;
Session["ExcelFileName"] = strFileName;
Session["ExcelFileAvailable"] = null;

string[] strSep = strFileName.Split('.');
int arrLength = strSep.Length - 1;
string strExt = strSep[arrLength].ToString().ToUpper();
string strPathToUpload = Server.MapPath("ExcelFile");
string strPathToConvert = Server.MapPath("ExcelToHtmlFile");
string FileToUpload = strPathToUpload + "\\" + guidFileName + ".xls";
string FileToSave = strPathToConvert + "\\" + guidFileName + ".htm";
string ImageDirectory = "~/ExcelToHtmlFile/" + guidFileName + "_files/";
string FileListXML = "filelist.xml";
string FileListPath = Server.MapPath(ImageDirectory + FileListXML);

Check if type of file is excel
Expand|Select|Wrap|Line Numbers
  1. if (strExt.ToUpper().Equals("XLS"))
  2. {
This is the hardest part of all..Save the excel file as html...
Expand|Select|Wrap|Line Numbers
  1. object missing = System.Reflection.Missing.Value;
  2. excelZipUpload.PostedFile.SaveAs(FileToUpload);
  3. //open the file internally in excel. In the method all the parameters should be passed by object reference
  4. Workbook objWorkbook = objExcel.Workbooks.Open(FileToUpload, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
  5. //Do the background activity
  7. objWorkbook.SaveAs(FileToSave, XlFileFormat.xlHtml, missing, missing, missing, missing,
  8. XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
Then close the excel file
Expand|Select|Wrap|Line Numbers
  2. //Close/quit excel
  3. objExcel.Quit();
When we save the excel file as html, there will be an xml document contains list of images file, then we can take the image file name from that document.
Expand|Select|Wrap|Line Numbers
  1. XmlDocument xDoc = new XmlDocument();
  2. xDoc.Load(FileListPath);
  4. bool sleepedOnce = false;
  5. if (xDoc.ChildNodes[0] != null)
  6. {
  7.    foreach (XmlNode xNode in xDoc.ChildNodes[0].ChildNodes)
  8.    {
  9.       if (xNode.Attributes["HRef"] != null)
  10.       {
  11.            string attrName = xNode.Attributes["HRef"].Value;
  12.            if (attrName.Contains(".gif"))
  13.            {
  14.                imageCollection.Add("~/ExcelToHtmlFile/" + guidFileName + "_files/" + attrName);
  15.            }
  16.            else
  17.            {
  18.                if (!sleepedOnce)
  19.                {
  20.                    Thread.Sleep(1000);
  21.                    sleepedOnce = true;
  22.                }
  24.                FileInfo fInfo = new FileInfo(Server.MapPath(ImageDirectory + attrName));
  25.                fInfo.Delete();
  26.            }
  27.         }
  28.    }
  29. }
Ps: we use thread here to pending the process of deletion, because sometimes the deletion can't be done because they still "think" that the file still in use..

Then save the list of string that contains images path file into session
Expand|Select|Wrap|Line Numbers
  1. Session["GraphList"] = imageCollection;
  2. }
So, that's how we make it....
But then there is another problem, apparently, we can't install excel application in the server. So, I am going to ask again....hehehehehe..
Are there any other way, we can run this script in a computer which has no excel application in it?
Thank you very much..
Arigatou gozaimasu..
Xie xie..
Danke schön!
Feb 19 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Al Franz | last post: by
12 posts views Thread by middletree | last post: by
1 post views Thread by Matt | last post: by
1 post views Thread by Lucas Tam | last post: by
2 posts views Thread by David Scemama | last post: by
1 post views Thread by Psapg | last post: by
3 posts views Thread by cmdolcet69 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.