DataTable针对xml、excel、csv导入和导出

此段代码是针对DataTable 对xml、excel、csv 对文件的导入和导出功能,记录一下,以供以后使用。

  一定要导入excel 并添加引用Microsoft.Office.Interop.Excel 11.0版本。

  Default.aspx.cs文件

  using System;

  using System.Collections.Generic;

  using System.Linq;

  using System.Web;

  using System.Web.UI;

  using System.Web.UI.WebControls;

  using System.Data.SqlClient;

  using System.Data;

  using System.Xml;

  using System.Xml.Xsl;

  using System.IO;

  using System.Data.OleDb;

  using System.Data.Odbc;

  using System.Text;

  using Excel = Microsoft.Office.Interop.Excel;

  namespace fantest

  {

  public partial class _Default : System.Web.UI.Page

  {

  protected void Page_Load(object sender, EventArgs e)

  {

  Bind();

  }

  protected void Bind()

  {

  this.GridView1.DataSource = this.GetDataTable();

  this.GridView1.DataBind();

  }

  private DataTable GetDataTable()

  {

  DataSet ds = new DataSet();

  using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=test"))

  {

  string sql = "select * from InfoTable where 1=1";

  SqlDataAdapter dap = new SqlDataAdapter(sql, conn);

  dap.Fill(ds,"InfoTable");

  }

  return ds.Tables["InfoTable"];

  }

  //TO XML

  protected void Button1_Click(object sender, EventArgs e)

  {

  DataTable dt = this.GetDataTable();

  StringBuilder sb = new StringBuilder();

  sb.Append("<" + dt.TableName + ">");

  foreach (DataRow row in dt.Rows)

  {

  sb.Append("<item>");

  for (int i = 0; i < dt.Columns.Count; i++)

  {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C# 中,您可以使用以下代码实现 DataGridView 数据的导入导出 Excel 文件的功能。 导入 Excel 文件到 DataGridView: ```csharp using System; using System.Data; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; public void ImportExcelToDataGridView(DataGridView dataGridView, string filePath) { Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Open(filePath); Excel.Worksheet worksheet = workbook.ActiveSheet; int rowCount = worksheet.UsedRange.Rows.Count; int columnCount = worksheet.UsedRange.Columns.Count; DataTable dataTable = new DataTable(); for (int col = 1; col <= columnCount; col++) { dataTable.Columns.Add(worksheet.Cells[1, col].Value.ToString()); } for (int row = 2; row <= rowCount; row++) { DataRow dataRow = dataTable.NewRow(); for (int col = 1; col <= columnCount; col++) { dataRow[col - 1] = worksheet.Cells[row, col].Value; } dataTable.Rows.Add(dataRow); } dataGridView.DataSource = dataTable; workbook.Close(); excelApp.Quit(); } ``` 导出 DataGridView 数据到 Excel 文件: ```csharp using System; using System.Data; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; public void ExportDataGridViewToExcel(DataGridView dataGridView, string filePath) { Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Add(); Excel.Worksheet worksheet = workbook.ActiveSheet; for (int col = 1; col <= dataGridView.Columns.Count; col++) { worksheet.Cells[1, col] = dataGridView.Columns[col - 1].HeaderText; worksheet.Cells[1, col].Font.Bold = true; } for (int row = 0; row < dataGridView.Rows.Count; row++) { for (int col = 0; col < dataGridView.Columns.Count; col++) { worksheet.Cells[row + 2, col + 1] = dataGridView.Rows[row].Cells[col].Value.ToString(); } } worksheet.Columns.AutoFit(); workbook.SaveAs(filePath); workbook.Close(); excelApp.Quit(); } ``` 您可以调用 `ImportExcelToDataGridView` 方法将 Excel 文件导入到 DataGridView,传入要导入的 DataGridView 对象和 Excel 文件路径。调用 `ExportDataGridViewToExcel` 方法将 DataGridView 数据导出Excel 文件,传入要导出的 DataGridView 对象和 Excel 文件路径。 请注意,导入导出 Excel 文件需要在项目中引用 Microsoft.Office.Interop.Excel 库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值