• COM 自动化控制Excel应用程序


    class Program
        {
            static void Main(string[] args)
            {
                var dt = new System.Data.DataTable();
    
                dt.Columns.Add("#");
    
                dt.Columns.Add("Expiration Date");
    
                dt.Columns.Add("Remark");
    
                DataRow dr = dt.NewRow();
    
                dr[0] = 1;
                dr[1] = "23/9/2015";
                dr[2] = "For H&M (Shanghai DC) use. H&M is using EPV and it does not have license control function, this license file is for record only. ";
    
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
    
                dr[0] = 2;
                dr[1] = "23/12/2015";
                dr[2] = "For SZ SI internal testing. ";
    
                dt.Rows.Add(dr);
    
                ExportToExcel(dt);
            }
    
            /// <summary>
            /// 导出Excel
            /// </summary>
            /// <param name="dt">要导出的DataTable</param>
            private static void ExportToExcel(System.Data.DataTable dt)
            {
                if (dt == null) return;
                var xlApp = new Application();
                xlApp.Visible = true;
    
                var workBook = xlApp.Workbooks.Add(Type.Missing);
                //从1开始
                Worksheet workSheet = workBook.Sheets[1];
                Range range;
    
                //写入标题
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    workSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
                    range = (Range)workSheet.Cells[1, i + 1];
                    range.Interior.ColorIndex = 31;//背景颜色
                    range.Font.ColorIndex = 2;
                    range.HorizontalAlignment = XlHAlign.xlHAlignCenter;//居中
                    //加边框
                    range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
                    range.EntireColumn.ColumnWidth = 30;
                    range.EntireRow.AutoFit();//自动调整行高
                }
                //写入内容
                for (int r = 0; r < dt.Rows.Count; r++)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        workSheet.Cells[r + 2, i + 1] = dt.Rows[r][i];
                        range = (Range)workSheet.Cells[r + 2, i + 1];
                        range.Font.Size = 9;//字体大小
                        range.HorizontalAlignment = XlHAlign.xlHAlignCenter;//居中
                        range.VerticalAlignment = XlHAlign.xlHAlignCenter;
                        //加边框
                        range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic, null);
                        range.EntireRow.AutoFit();//自动调整行高
                        range.WrapText = true;
                        if (i == dt.Columns.Count - 1)
                        {
                            range.HorizontalAlignment = XlHAlign.xlHAlignLeft;
                        }
                    }
                }
            }
    
        }
  • 相关阅读:
    面试准备
    论文投稿Cover letter
    Pycharm 快捷键
    linux下常用命令:
    Qt中数据模块学习
    Qt 多线程和网络编程学习
    VS高效开发快捷键
    良好编码风格习惯整理
    Qt QAxObject操作excel文件过程总结(转):
    Qt开发中的实用笔记三--关于各种类的零碎知识点:
  • 原文地址:https://www.cnblogs.com/JustYong/p/4554899.html
Copyright © 2020-2023  润新知