• C#导入Excel到Dataset和导出Excel到DataTable


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.OleDb;
    using System.Windows.Forms;
    using Microsoft.Office.Interop.Excel;

    namespace WinExcelOperate
    {
        class ExcelOperate
        {
            //加载Excel   
            public static DataSet LoadDataFromExcel(string filePath)
            {
                try
                {
                    string strConn;
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
                    OleDbConnection OleConn = new OleDbConnection(strConn);
                    OleConn.Open();
                    String sql = "SELECT * FROM  [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等   

                    OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
                    DataSet OleDsExcle = new DataSet();
                    OleDaExcel.Fill(OleDsExcle, "Sheet1");
                    OleConn.Close();
                    return OleDsExcle;
                }
                catch (Exception err)
                {
                    MessageBox.Show("数据绑定Excel失败!失败原因:" + err.Message, "提示信息",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return null;
                }
            }


            public static void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
            {
                if (tmpDataTable == null)
                    return;
                int rowNum = tmpDataTable.Rows.Count;
                int columnNum = tmpDataTable.Columns.Count;
                int rowIndex = 1;
                int columnIndex = 0;

                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                xlApp.DefaultFilePath = "";
                xlApp.DisplayAlerts = true;
                xlApp.SheetsInNewWorkbook = 1;
                Workbook xlBook = xlApp.Workbooks.Add(true);

                //将DataTable的列名导入Excel表第一行
                foreach (DataColumn dc in tmpDataTable.Columns)
                {
                    columnIndex++;
                    xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
                }

                //将DataTable中的数据导入Excel中
                for (int i = 0; i < rowNum; i++)
                {
                    rowIndex++;
                    columnIndex = 0;
                    for (int j = 0; j < columnNum; j++)
                    {
                        columnIndex++;
                        xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();
                    }
                }
                //xlBook.SaveCopyAs(HttpUtility.UrlDecode(strFileName, System.Text.Encoding.UTF8));
                xlBook.SaveCopyAs(strFileName);
            }
        }
    }

  • 相关阅读:
    C# decimal保留指定的小数位数,不四舍五入
    C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示)
    手机游戏模拟器汇总 用于开发
    WinAPI 操作串口
    C#图片压缩算法
    SQL SERVER 2008 无法启动TSQL调试的解决方法
    C#放缩、截取、合并图片并生成高质量新图的类
    C#图片处理之: 另存为压缩质量可自己控制的JPEG
    URL及short URL短网址
    1的补码及2的补码
  • 原文地址:https://www.cnblogs.com/yuxuetaoxp/p/2626323.html
Copyright © 2020-2023  润新知