码迷,mamicode.com
首页 > 编程语言 > 详细

VBA

时间:2015-06-05 17:05:02      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

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;
                    }
                }
            }
        }

    }

 

VBA

标签:

原文地址:http://www.cnblogs.com/JustYong/p/4554899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!