Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 导入Excel文件_C# - Fatal编程技术网

C# 导入Excel文件

C# 导入Excel文件,c#,C#,如何将Excel文件导入C project?确保它位于project目录中的正确文件夹中,然后,假设您使用的是Visual Studio,请单击“在解决方案资源管理器中显示所有文件”这是工具顶部的一个小按钮,然后右键单击现在应该显示的ecel文件,然后单击“添加到项目”。您可以使用Office对象模型处理Excel工作表。添加对的引用。您可以使用“引用”来处理excel工作表。注意,excel文件的路径是动态设置的: // using System.Data.OleDb OleDbConnect

如何将Excel文件导入C project?

确保它位于project目录中的正确文件夹中,然后,假设您使用的是Visual Studio,请单击“在解决方案资源管理器中显示所有文件”这是工具顶部的一个小按钮,然后右键单击现在应该显示的ecel文件,然后单击“添加到项目”。

您可以使用Office对象模型处理Excel工作表。添加对的引用。您可以使用“引用”来处理excel工作表。

注意,excel文件的路径是动态设置的:

// using System.Data.OleDb
OleDbConnection ExcelConection = null;
OleDbCommand ExcelCommand = null;
OleDbDataReader ExcelReader = null;
OleDbConnectionStringBuilder OleStringBuilder = null;

try
{
    OleStringBuilder =
        new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
    OleStringBuilder.DataSource = MapPath(@"~\App_Datav\MyExcelWorksheet.xls");

    ExcelConection = new OleDbConnection();
    ExcelConection.ConnectionString = OleStringBuilder.ConnectionString;

    ExcelCommand = new OleDbCommand();
    ExcelCommand.Connection = ExcelConection;
    ExcelCommand.CommandText = "Select * From [Sheet1$]";

    ExcelConection.Open();
    ExcelReader = ExcelCommand.ExecuteReader();

    GridView1.DataSource = ExcelReader;
    GridView1.DataBind();
}
catch (Exception Args)
{
    LabelErrorMsg.Text = "Could not open Excel file: " + Args.Message;
}
finally
{
    if (ExcelCommand != null)
        ExcelCommand.Dispose();
    if (ExcelReader != null)
        ExcelReader.Dispose();
    if (ExcelConection != null)
        ExcelConection.Dispose();
}

您想将其添加到项目中还是将值读入C程序中?e、 g.将其转换为DataTable我们需要更多详细信息。你到底想干什么?你最终想要完成什么?我想在我的项目中编辑这些数据