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
-
using Microsoft.Office.Interop.Excel;
-
using System.Xml;
-
using System.IO;
-
using System.Threading;
-
And then..create an object to keep the excel file, this object will be an instance of class ApplicationClass.
-
ApplicationClass objExcel = new ApplicationClass();
-
Define the list of string to keep path of images file.
-
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.
-
if ((excelZipUpload.PostedFile != null) && (excelZipUpload.PostedFile.ContentLength > 0))
-
{
-
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.
-
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
[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
-
if (strExt.ToUpper().Equals("XLS"))
-
{
-
This is the hardest part of all..Save the excel file as html...
-
object missing = System.Reflection.Missing.Value;
-
excelZipUpload.PostedFile.SaveAs(FileToUpload);
-
//open the file internally in excel. In the method all the parameters should be passed by object reference
-
Workbook objWorkbook = objExcel.Workbooks.Open(FileToUpload, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
-
//Do the background activity
-
-
objWorkbook.SaveAs(FileToSave, XlFileFormat.xlHtml, missing, missing, missing, missing,
-
XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
-
Then close the excel file
-
-
//Close/quit excel
-
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.
-
XmlDocument xDoc = new XmlDocument();
-
xDoc.Load(FileListPath);
-
-
bool sleepedOnce = false;
-
if (xDoc.ChildNodes[0] != null)
-
{
-
foreach (XmlNode xNode in xDoc.ChildNodes[0].ChildNodes)
-
{
-
if (xNode.Attributes["HRef"] != null)
-
{
-
string attrName = xNode.Attributes["HRef"].Value;
-
if (attrName.Contains(".gif"))
-
{
-
imageCollection.Add("~/ExcelToHtmlFile/" + guidFileName + "_files/" + attrName);
-
}
-
else
-
{
-
if (!sleepedOnce)
-
{
-
Thread.Sleep(1000);
-
sleepedOnce = true;
-
}
-
-
FileInfo fInfo = new FileInfo(Server.MapPath(ImageDirectory + attrName));
-
fInfo.Delete();
-
}
-
}
-
}
-
}
-
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
-
Session["GraphList"] = imageCollection;
-
}
-
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!