如何在C#.net中打开Excel XLS文件 [英] How to open Excel XLS file in C#.net

查看:128
本文介绍了如何在C#.net中打开Excel XLS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以回答如何打开和读取上载的Excel文件并以Web形式以C#形式在网页上显示该文件.

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.

推荐答案

打开并读取XLS单元格,请按照步骤
1.从"COM"选项卡提供Interop.Excel对象的引用
2.在您的项目中编写以下代码
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
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);
}


在以下位置显示MS Excel表格和图表使用C#的ASPX页面 [ ^ ]
http://support.microsoft.com/kb/306572 [
Display MS Excel Sheets and Charts in ASPX Pages using C#[^]
http://support.microsoft.com/kb/306572[^]


请尝试以下代码.

Try as below code.

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();
  }


这篇关于如何在C#.net中打开Excel XLS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆