472,808 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,808 software developers and data experts.

Grabbing graphic from excel file and display it as thumbnails

roswara
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 2015
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

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

Similar topics

11
by: Al Franz | last post by:
Have a large graphic file to display in an IMG SRC tag. How can you have it quickly display a dummy small graphic first while it waits for the full large file size to load in the same place in the...
12
by: middletree | last post by:
I read through Curt's sample code for using ASP to create a spreadsheet (.xls). I think I understand the basics, but am still running into problems. I'm doing this on an Intranet app, so I saved...
1
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table....
1
by: Lucas Tam | last post by:
Is there a .NET component which can generate a Graphic File (JPEG, GIF, BMP, etc) from a URL? Basically I want to create thumbnails of several dynamic pages I have (via code of course). ...
0
by: Eric | last post by:
I have this same problem, and I've *half-way* resolved it. It turns out that this is not exactly an IO problem; it's actually a security issue and maybe even an ASP.NET bug. Here is what I've...
2
by: David Scemama | last post by:
Hi, I'm looking for a way to display semi graphic characters in a multi line text control or in a rich text control. I've tried with all the characters of the extended ASCII table (code page...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
1
by: Psapg | last post by:
Hi! I'm new to javasript, and i must confess to have borowed a few free scripts from the net to satisfie my needs.... Still i can't find even an idea of ascipt to do this... Please Help!!! ...
3
by: cmdolcet69 | last post by:
What would be the easiest way to export a graphic (I have yet to determine what type of graphic, .jpg, bitmap....) however im open to suggestion, also how can i export my graphic so that it appear...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.