• DataTable数据导入到Excel 引用Microsoft Office 12.0 Object Library和 Microsoft Excel 14.0 Object Library 并且需要注意的问题


            /// <summary>
            /// 将DataTalbe导出到Excel中
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="ProjectName">生成的Excel的Sheet的名字</param>
            /// <param name="filePath">保存的路径</param>
            public static void Export(System.Data.DataTable dt, string filePath)
            {
                if (dt == null)
                {
                    throw new Exception("数据表中无数据");
                }
                int eRowIndex = 1;
                int eColIndex = 1;
                int cols = dt.Columns.Count;
                int rows = dt.Rows.Count;
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
                try
                {
                    //列名的处理
                    for (int i = 0; i < cols; i++)
                    {
                        xlApp.Cells[eRowIndex, eColIndex] = dt.Columns[i].ColumnName;
                        eColIndex++;
                    }
                    //列名加粗显示
                    xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[eRowIndex, cols]).Font.Bold = true;
                    xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Name = "Arial";
                    xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Size = "10";
                    eRowIndex++;
    
                    for (int i = 0; i < rows; i++)
                    {
                        eColIndex = 1;
                        for (int j = 0; j < cols; j++)
                        {
                            xlApp.Cells[eRowIndex, eColIndex] = dt.Rows[i][j].ToString();
                            eColIndex++;
                        }
                        eRowIndex++;
                    }
                    //控制单元格中的内容。
                    xlApp.Cells.EntireColumn.AutoFit();
    
                    xlApp.DisplayAlerts = false;
                    xlBook.SaveCopyAs(filePath);
                    xlApp.Workbooks.Close();
                }
                catch
                {
                    throw;
                }
                finally
                {
                    xlApp.Quit();
                    //杀掉Excel进程。
                    GC.Collect();
                }
            }
    

     注意:using Microsoft.Office.Interop.Excel;  VS2010 下 添加引用  COM选项中找Microsoft Office 12.0 Object Library  Microsoft Excel 14.0 Object Library

    引用目录下出现Microsoft.Office.Core     Microsoft.Office.Interop.Excel

    如果代码中出现  Excel.ApplicationClass()无法互嵌套操作类型 请改用适用的接口 

    把引用的Microsoft.Office.Interop.Excel 右击属性 嵌入互操作类型改为false即可

  • 相关阅读:
    java第19次
    Unity3D常用的生命周期函数
    Unity3D结合ZXing生成中间带图标的二维码并保存
    Unity查看当前内存使用情况(针对移动端开发)
    Unity 获取鼠标悬停位置下的UI或3D物体对象
    【Unity3D】获取鼠标在三维空间(世界坐标系)的位置
    【Unity3D—C#】按下任意按键,返回按键的名称 以及 KeyCode键码详解
    Unity UGUI获取鼠标在屏幕的准确点击位置
    ML-Agent:通过TF#使用训练完成的模型数据
    对《ml-agent:Win10下环境安装》更新
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/2738495.html
Copyright © 2020-2023  润新知