Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
can anybody answer how to open and read uploaded Excel file and show that file on the web page in C# in the web form.
Posted

Open and Read XLS cellwise, follow steps
1. give the reference of Interop.Excel object from COM tab
2. write following code in your project
C#
Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
    openFileDialog1.FileName, 0, true, 5,
     "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
     0, true);
Excel.Sheets sheets = theWorkbook.Worksheets;
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
for (int i = 1; i <= 10; i++)
{
   Excel.Range range = worksheet.get_Range("A"+i.ToString(), "J" + i.ToString());
   System.Array myvalues = (System.Array)range.Cells.Value;
   string[] strArray = ConvertToStringArray(myvalues);
}
 
Share this answer
 
Comments
Philippe Mori 3-Sep-11 8:55am    
Excel interop is not recommanded on server as it does not scale well and if you use shared hosting, you won't have Office on the server.
 
Share this answer
 
v2
Comments
Oshtri Deka 2-Sep-11 6:51am    
Good starting point.
nangloulaing 6-Sep-11 0:18am    
Thanks.. I just getting started with this field and i need lots of helps..
Prerak Patel 6-Sep-11 1:08am    
You are welcome
Try as below code.

C#
string excelFilePath = "YourExcelFilePath";
System.IO.FileInfo file = new System.IO.FileInfo(excelFilePath);

  if (file.Exists)
  {
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename="  +      file.Name);
    Response.AddHeader("Content-Length", file.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.WriteFile(file.FullName);
    Response.End();
  }
 
Share this answer
 
Comments
Vijaya Sekhar Sreeram 7-Mar-13 22:54pm    
so much thanks friend really helpfull code thank you so much..........
TutuRanjan 27-Jul-13 2:46am    
process.start("Excels",""" fileName""")
Open excel file
RaisKazi 14-Mar-13 17:35pm    
Glad to know it helped you!
If you want to read the data from an uploaded file, then you can uses Open XML SDK 2.0[^]

For read access, it should works very well.

For creating or modifying document it might be hard to use properly compared to most other alternative as it is very low level (you have to know a lot of details to generate a valid file).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900