Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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# 在asp.net C中从数据集中导出excel文件#_C#_Asp.net - Fatal编程技术网

C# 在asp.net C中从数据集中导出excel文件#

C# 在asp.net C中从数据集中导出excel文件#,c#,asp.net,C#,Asp.net,我想用asp.net C#将sql server中的数据导出为数据。演示如何使用单个数据集和两个不同的数据适配器将数据从sql server提取到Excel电子表格中 // fill data from SQL var ConnSql= new System.Data.OleDb.OleDbConnection(ConnStringSql); var da1 = new System.Data.OleDb.OleDbDataAdapter(); da1.SelectComman

我想用asp.net C#

将sql server中的数据导出为数据。演示如何使用单个数据集和两个不同的数据适配器将数据从sql server提取到Excel电子表格中

  // fill data from SQL
  var ConnSql= new System.Data.OleDb.OleDbConnection(ConnStringSql);

  var da1 = new System.Data.OleDb.OleDbDataAdapter();
  da1.SelectCommand=  new System.Data.OleDb.OleDbCommand(sqlSelect);
  da1.SelectCommand.Connection= ConnSql;

  var ds1 = new System.Data.DataSet();
  da1.Fill(ds1, "Extracto");

  // need to update the rows so the DA does the insert...
  foreach (System.Data.DataRow r in ds1.Tables[0].Rows) { 
    r["Extracted"]= System.DateTime.Now; // update one column in each row
  }

  // insert into Excel
  var ConnExcel= new System.Data.OleDb.OleDbConnection(ConnStringExcel);
  var da2 = new System.Data.OleDb.OleDbDataAdapter();

  da2.UpdateCommand= new System.Data.OleDb.OleDbCommand(sqlInsert);
  da2.UpdateCommand.Connection= ConnExcel;

  da2.UpdateCommand.Parameters.Add("@ProductId", System.Data.OleDb.OleDbType.Integer, 4, "ProductId");
    ..etc..

  da2.Update(ds1, "Extracto");

您还可以将其导出到CSV文件中,以逗号“,”或其他分隔符分隔。为此,您必须使用StreamWriter对象来编写文本文件并在数据集中循环。

电子表格设备对于这类事情来说是一个非常好的工具-非常快速且易于使用。我只希望他们有一个便宜一点的版本,价格不到1000美元,但似乎做到了广告宣传的一切

不仅可以导出数据,还可以导出格式、颜色、字体和图形