By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,470 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

Grabbing graphic from excel file and display it as thumbnails

roswara
P: 5
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
Share this Question
Share on Google+
1 Reply


roswara
P: 5
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;
  5.  
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();
  2.  
Define the list of string to keep path of images file.
Expand|Select|Wrap|Line Numbers
  1. List<string> imageCollection = new List<string>();
  2.  
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.         {
  3.  
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();
  2.  
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
[code]
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. {
  3.  
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
  6.  
  7. objWorkbook.SaveAs(FileToSave, XlFileFormat.xlHtml, missing, missing, missing, missing,
  8. XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
  9.  
Then close the excel file
Expand|Select|Wrap|Line Numbers
  1.  
  2. //Close/quit excel
  3. objExcel.Quit();
  4.  
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);
  3.  
  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.                }
  23.  
  24.                FileInfo fInfo = new FileInfo(Server.MapPath(ImageDirectory + attrName));
  25.                fInfo.Delete();
  26.            }
  27.         }
  28.    }
  29. }
  30.  
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. }
  3.  
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.