C# 操作EXCEL样式 示例 --生成EXCEL审计表

黄舟
发布: 2017-03-02 11:41:33
原创
1445 人浏览过

  C#  操作EXCEL样式 示例  --生成EXCEL审计表

 #region 生成审计表
        public static void GenerateAccountGrid(
        string fileName, 
        string tableName, 
        string companyName, 
        string owner, 
        DateTime endline, 
        string copyMan, AnalysisEnt ent)
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(tableName);
                dt.Columns.Add("");
                dt.Columns.Add("");
                dt.Columns.Add("");
                dt.Columns.Add("");
                dt.Columns.Add("");
                //AddNewRow(ref dt, 6, "", "", "", tableName, "", "");
                AddNewRow(ref dt, 6, "被审计单位名称:", companyName, "编制人:" + owner, "", "日期: " + DateTime.Now.ToShortDateString(), "");
                AddNewRow(ref dt,  6, "会计期间或截止日期:" + endline, "", "复制人:" + copyMan, "", "索引号:" + "YIZK-" + DateTime.Now.Millisecond.ToString());
                AddNewRow(ref dt, 6, " ", " ", " ", " ", "页次: 1", "");
                AddNewRow(ref dt, 6, "项目名称", "期末未审计数", "摘要", "账项调整", "重分类调整", "期末审定数");
                AddNewRow(ref dt, 6, ent.ProName, ent.UnFinishNum, ent.FinishNum, ent.Summary, "0", "0", ent.FinishNum);
                AddNewRow(ref dt, 6, "审计结论:", " ", " ", " ", " ", " ");
                ExcelHelper.ExportToExcel(dt, fileName, tableName);


                if (File.Exists(fileName))
                {


                    Microsoft.Office.Interop.Excel.Application xlsApp = new Microsoft.Office.Interop.Excel.Application();
                    Microsoft.Office.Interop.Excel.Workbook workbook = xlsApp.Workbooks.Open(fileName, Type.Missing, 
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    Microsoft.Office.Interop.Excel.Worksheet mySheet = workbook.Sheets[tableName] as Microsoft.Office.Interop.Excel.Worksheet;


                    xlsApp.Visible = true;


                    ////标题样式
                    ((Microsoft.Office.Interop.Excel.Range)mySheet.Columns["A:F", System.Type.Missing]).ColumnWidth = 20;
                    Microsoft.Office.Interop.Excel.Range titleRange = mySheet.get_Range("A1", "F1");
                    titleRange.ClearContents();
                    titleRange.Merge(0);
                    titleRange = mySheet.get_Range("A1", "A1");
                    titleRange.Cells[1, 1] = tableName;
                    titleRange.Font.Size = 30;
                    titleRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                    titleRange.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).
                    Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium;


                    ////为所有单元格添加边框
                    Microsoft.Office.Interop.Excel.Range allRange = mySheet.get_Range("A1", "F7");
                    allRange.Borders.LineStyle = 1;
                    allRange.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).
                    LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;


                    ////最后一行单元格合并
                    Microsoft.Office.Interop.Excel.Range contentRange = mySheet.get_Range("A7", "F7");
                    contentRange.ClearContents();
                    contentRange.Merge(0);
                    contentRange = mySheet.get_Range("A7", "F7");
                    contentRange[1, 1] = "审计结论:";
                    contentRange.RowHeight = 30;
                    contentRange.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignTop;
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }




        public static void AddNewRow(ref DataTable dt, int columnCount, params string[] cells)
        {
            if (cells == null || cells.Length > columnCount)
            {
                return;
            }
            DataRow dr = dt.NewRow();
            for (int i = 0; i < cells.Length; i++)
            {
                dr[i] = cells[i];
            }
            dt.Rows.Add(dr);
        }
        #endregion
登录后复制

 以上就是C#  操作EXCEL样式 示例  --生成EXCEL审计表的内容,更多相关内容请关注PHP中文网(www.php.cn)!


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板