博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第522篇--DataTable to Excel C#
阅读量:4348 次
发布时间:2019-06-07

本文共 3650 字,大约阅读时间需要 12 分钟。

The code is quite simpke, just have a look.

Add reference to the following dlls:

Interop.Microsoft.Office.Core.dll

Interop.Microsoft.Office.Interop.Excel.dll

Download dlls:

class Program    {        static void Main(string[] args)        {            System.Data.DataTable table = new System.Data.DataTable();            DataColumn column1 = new DataColumn() { ColumnName="Column1"};            DataColumn column2 = new DataColumn() { ColumnName = "Column2" };            table.Columns.Add(column1);            table.Columns.Add(column2);            table.Rows.Add("1","2");            table.Rows.Add("3", "4");            DataTabletoExcel(table, @"C:\temp.XLS");        }        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;            Application xlApp = new 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);        }    }

 

Excel to Data:

private static  System.Data.DataTable ExportFromExcelToDataTable()        {            System.Data.DataTable excelDataTable = new System.Data.DataTable();            string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0";            // Create Connection to Excel Workbook            using (System.Data.OleDb.OleDbConnection excelConnection =                  new System.Data.OleDb.OleDbConnection(excelConnectionString))            {                excelConnection.Open();                System.Data.DataTable dt = excelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);                string[] excelSheet = new String[dt.Rows.Count];                int sheet = 0;                foreach (DataRow row in dt.Rows)                {                    excelSheet[sheet] = row["Table_Name"].ToString();                    sheet++;                }                excelDataTable.Clear();                for (int i = 0; i < excelSheet.Length; i++)                {                    System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand                         ("Select  * FROM [" + excelSheet[i] + "]", excelConnection);                    System.Data.OleDb.OleDbDataAdapter excelAdapter = new System.Data.OleDb.OleDbDataAdapter();                    excelAdapter.SelectCommand = command;                    excelAdapter.Fill(excelDataTable);                }                excelConnection.Close();            }            return excelDataTable;        }

 

转载于:https://www.cnblogs.com/shanghaijimzhou/archive/2013/03/25/2981503.html

你可能感兴趣的文章
多实例Mysql配置
查看>>
CentOS6.5桌面版安装VirtualBox提示错误/etc/init.d/vboxdrv setup
查看>>
KOA中间件源码解析
查看>>
构建之法阅读笔记03
查看>>
jquery 点击切换值
查看>>
vue+element前端自行分页
查看>>
C#操作XML
查看>>
tkinter学习02
查看>>
Mapnik使用postgres中的栅格数据
查看>>
html基本知识
查看>>
IOS手势不识别
查看>>
IOS网络编程之请求内容
查看>>
爬虫——为什么有代理
查看>>
HDU 1599 find the mincost route(floyd求最小环 无向图)
查看>>
vue v-for循环checkbox存在的问题
查看>>
hibernate模糊查询-Restrictions.ilike & Expression.like
查看>>
二分·归并排序之逆序对 算法讲解和题目
查看>>
python @property
查看>>
J2SE基础夯实系列之String字符串不可变的理解,不可变类,final关键字到底修饰了什么...
查看>>
2.7 UML状态图
查看>>