DataTable导入导出Excel

//导入   
protected DataSet GetExcelContent(string filepath)
    {
        string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1'";
        System.Data.OleDb.OleDbConnection myConn = new System.Data.OleDb.OleDbConnection(strCon);

        string strCom = "SELECT 0 as ID , ID as GoodsID,编码 as Code,名称 as Name,产品型号 as Model,单位 as Unit,账面库存 as BQ,0 as Quantity,'' as OI,货号 as ImgNum,'0' AS rIndex  FROM [盘点单$]";
        myConn.Open();
        System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, myConn);
        //创建一个DataSet对象  
        DataSet myDataSet = new DataSet();
        //得到自己的DataSet对象  
        myCommand.Fill(myDataSet, "table1");
        //关闭此数据链接  
        myConn.Close();
        for (int i = 0; i < myDataSet.Tables["table1"].Rows.Count; i++)
        {
            myDataSet.Tables["table1"].Rows[i]["rIndex"] = i.ToString();
        }
        return myDataSet;
    }

//导出
protected void lblUxToExcel_Click(object sender, EventArgs e)
    {
        StringWriter sw = new StringWriter();
        sw.WriteLine("编号\t名称\t密码\t性别\tEmail\t城市\t地址\t登陆IP");
        DataTable dt = userManager.FindAlluser();
        foreach (DataRow dr in dt.Rows)
        {
            sw.WriteLine(dr["userId"] + "\t" + dr["userName"] + "\t" + dr["password"] + "\t" + dr["sex"] + "\t" + dr["email"] + "\t" + dr["city"] + "\t" + dr["address"] + "\t" + dr["loginIP"]);
        }
        sw.Close();
        Response.AddHeader("Content-Disposition", "attachment; filename=test.xls");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Write(sw);
        Response.End();
    }

转载于:https://www.cnblogs.com/myssh/archive/2009/08/10/1543157.html

  • 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、付费专栏及课程。

余额充值