We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Open excel file without saving to harddisk (c#)

Hi there,

How can i open and get values from an excel file that is post by user using the file upload component from asp.net. I do not want to save the file to the server harddisk. I just need to grab the values and store them in the database. Main thing here is how to open and grab the values without saving the excel file. By the way, im doing this using c#. Please provide me with a working example as this is quite urgent. Thank in advance.

If you are going to show me the following code, please tell me how to get the file name from the file upload component. Because the documentation is showing hard code value for the file name. Please note that I also need to know how to loop through to get the values.

FileStream fstream =
new FileStream(“C:\book1.xls”, FileMode.Open);


workbook.Open(fstream);


Regards,
Jason

Hi,

I think you may get the posted file (by FileUpload control) into streams and then obtain the values in the different cells in the worksheet (looping through the cells) for your need. You can also export the data of a complete worksheet to fill a datatable or even an array for your need too which you may later update your database based on the values in the datatable.

See the sample code below:
Sample code:
Workbook workbook = new Workbook();
workbook.Open(FileUpload1.PostedFile.InputStream);
//Get the first worksheet cells collection
Cells cells = workbook.Worksheets[0].Cells;

for (int i = 0; i < cells.MaxDataRow; i++)
{
for (int j = 0; j < cells.MaxDataColumn; j++)
{
Response.Write(cells[i, j].StringValue);

}
}

//You can export the data of a complete worksheet to fill a data table if you want.
DataTable dt = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1);

//Your code goes here



Thank you.

It works. Thanks alot Amjad Sahi.

hi,


But yet this methode is depriciate… how can i open the file direct after save ?

            workbook.Save(destinationFolder, reportFile);
//Create a Stream object
FileStream fstream = new FileStream(Path.Combine(destinationFolder, reportFile), FileMode.Open);
        <span style="color:green;">//Creating a Workbook object, open the file from a Stream object</span>
        <span style="color:green;">//that contains the content of file and it should support seeking</span>
        workbook = <span style="color:blue;">new</span> <span style="color:#2b91af;">Workbook</span>(fstream);</pre></div>

Hi Julie,


Well, Workbook.Open() method is deprecated and you will now use Workbook’s constructor (as per your last line of code) to load/read the Excel spreadsheet into Aspose.Cells’ Workbook (object model). By the way, do you mean to say opening the final Excel file for display, for your information, Aspose.Cells is just a class library and does not offer any UI (Excel like Grid) for displaying/opening files into it. If you need this functionality to have independent Excel grid like environment/ means, you may use our Grid controls (desktop based and web based) to open the Spreadsheet into grid matrix in WYSIWYG (visual) manner), please see the docs for your reference:
http://www.aspose.com/docs/display/cellsnet/Aspose.Cells.GridDesktop
http://www.aspose.com/docs/display/cellsnet/Aspose.Cells.GridWeb


Thank you.