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);
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);
<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>
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:
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.