博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#操作EXCEL
阅读量:5114 次
发布时间:2019-06-13

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

    #region 初始化定义

            // 定义Application 对象,此对象表示整个Excel 程序
            Microsoft.Office.Interop.Excel.Application excelApp = null;
            // 定义Workbook对象,此对象代表工作薄
            Microsoft.Office.Interop.Excel.Workbook workBook = null;
            // 定义Worksheet 对象,此对象表示Execel 中的一张工作表
            Microsoft.Office.Interop.Excel.Worksheet workSheet = null;
            //定义Range对象,此对象代表单元格区域
            Microsoft.Office.Interop.Excel.Range excelRange;
            #endregion

            try

            {
                #region 创建实例
                //初始化 Application 对象 excelApp
                excelApp = new Microsoft.Office.Interop.Excel.Application();
                if (excelApp == null)
                {
                    return false;
                }
                excelApp.DisplayAlerts = false;
                //在工作薄的第一个工作表上创建任务列表
                workBook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets[1];
                // 命名工作表的名称为数据块名称
                workSheet.Name = dataBlock.NAME;
                //设置全部列宽
                workSheet.Columns.ColumnWidth = 15;
                workSheet.Columns.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
                workSheet.Cells.NumberFormat = "@";
                #endregion

                #region 设置数据块基本信息标题

                //合并单元格
                excelRange = workSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[1, 6]);
                excelRange.Merge(0);
                //单元格赋值
                workSheet.Cells[1, 1] = dataBlock.NAME + "数据块";
                //单元格设置
                excelRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //设置水平居中
                excelRange.RowHeight = 33;                                                               //设置单元格高度
                excelRange.Font.Bold = true;                                                             //设置是否加粗
                excelRange.Font.Size = 16;                                                               //设置字体大小
                excelRange.Font.Name = "黑体";                                                           //设置字体类型
                excelRange.Cells.Interior.Color = Color.LightGray.ToArgb();                              //设置背景色
                #endregion

                #region 设置数据块基本信息

                workSheet.Cells[2, 1] = "版本:";
                workSheet.get_Range(workSheet.Cells[2, 1], workSheet.Cells[2, 1]).Font.Bold = true;
                workSheet.Cells[2, 2] = dataBlock.REVISION_NUMBER.ToString("0.0");
                workSheet.Cells[2, 3] = "创建者:";
                workSheet.get_Range(workSheet.Cells[2, 3], workSheet.Cells[2, 3]).Font.Bold = true;
                workSheet.Cells[2, 4] = SingletonProvider<UsersBLL>.Instance.GetFullNameByUserID(dataBlock.CREATOR_ID);
                workSheet.Cells[2, 5] = "创建日期:";
                workSheet.get_Range(workSheet.Cells[2, 5], workSheet.Cells[2, 5]).Font.Bold = true;
                workSheet.Cells[2, 6] = dataBlock.CREATE_DATE.ToString();

                workSheet.Cells[3, 1] = "专业:";

                workSheet.get_Range(workSheet.Cells[3, 1], workSheet.Cells[3, 1]).Font.Bold = true;
                workSheet.Cells[3, 2] = SingletonProvider<CommonBLL>.Instance.GetSpecialtyName(dataBlock.SPECIALTY_ID);
                workSheet.Cells[3, 3] = "部件:";
                workSheet.get_Range(workSheet.Cells[3, 3], workSheet.Cells[3, 3]).Font.Bold = true;
                workSheet.Cells[3, 4] = SingletonProvider<CommonBLL>.Instance.GetPartName(dataBlock.PART_ID);
                workSheet.Cells[3, 5] = "阶段:";
                workSheet.get_Range(workSheet.Cells[3, 5], workSheet.Cells[3, 5]).Font.Bold = true;
                workSheet.Cells[3, 6] = SingletonProvider<CommonBLL>.Instance.GetStageName(dataBlock.STAGE_ID);

                workSheet.Cells[4, 1] = "状态:";

                workSheet.get_Range(workSheet.Cells[4, 1], workSheet.Cells[4, 1]).Font.Bold = true;
                workSheet.Cells[4, 2] = SingletonProvider<CommonBLL>.Instance.GetStateName(dataBlock.STATE_ID);

                //  [10/22/2010 Administrator] 添加数据定义工具的输出

                workSheet.Cells[4, 3] = "定义工具:";

                workSheet.get_Range(workSheet.Cells[4, 3], workSheet.Cells[4, 3]).Font.Bold = true;
                workSheet.Cells[4, 4] = SingletonProvider<CommonBLL>.Instance.GetToolsTypeName(dataBlock.OUTPUT_TYPE_ID) ;

                workSheet.Cells[4, 5] = "描述:";

                workSheet.get_Range(workSheet.Cells[4, 5], workSheet.Cells[4, 5]).Font.Bold = true;
                workSheet.Cells[4, 6] = dataBlock.DESCRIPTION;
                excelRange = workSheet.get_Range(workSheet.Cells[4, 6], workSheet.Cells[4, 8]);
                excelRange.Merge(0);
                excelRange.EntireRow.AutoFit();
                excelRange.WrapText = true;       //文本自动换行
               

                //给每个单元格加边框

                for (int i = 1; i < 5; i++)
                {
                    for (int j = 1; j < 7; j++)
                    {
                        excelRange = workSheet.get_Range(workSheet.Cells[i, j], workSheet.Cells[i, j]);
                        excelRange.Borders.LineStyle = 1;
                        excelRange.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous,
                    Microsoft.Office.Interop.Excel.XlBorderWeight.xlHairline,
                    Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                    Color.Black.ToArgb());
                    }
                }

                //设置边框

                excelRange = workSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[4, 6]);
                excelRange.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous,
                    Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium,
                    Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                    Color.Black.ToArgb());                                                               //单元格加边框 
                #endregion

                #region 设置数据项信息表头

                workSheet.Cells[6, 1] = "数据项名";
                workSheet.Cells[6, 2] = "父节点";
                workSheet.Cells[6, 3] = "节点";
                workSheet.Cells[6, 4] = "数据格式";
                workSheet.Cells[6, 5] = "数据类型";
                workSheet.Cells[6, 6] = "单值";
                workSheet.Cells[6, 7] = "单位";
                excelRange = workSheet.get_Range(workSheet.Cells[6, 1], workSheet.Cells[6, 7]);
                excelRange.RowHeight = 25;       //行高
                excelRange.Font.Name = "黑体";   //字体
                excelRange.Font.Bold = true;     //加粗
                excelRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //居中
                excelRange.Cells.Interior.Color = Color.LightGray.ToArgb(); //背景色
                #endregion
               
                #region 设置数据项信息
                if (dtItem != null && dtItem.Rows.Count > 0)
                {
                    int row = dtItem.Rows.Count;
                    int cell = dtItem.Columns.Count;
                    for (int i = 0; i < row; i++)
                    {
                        workSheet.Cells[7 + i, 1] = dtItem.Rows[i]["NAME"].ToString();
                        workSheet.Cells[7 + i, 2] = dtItem.Rows[i]["PARENT_NAME"].ToString() == "" ? dataBlock.NAME : dtItem.Rows[i]["PARENT_NAME"].ToString();
                        workSheet.Cells[7 + i, 3] = dtItem.Rows[i]["NODE_NAME"].ToString();
                        workSheet.Cells[7 + i, 4] = dtItem.Rows[i]["NODE_NAME"].ToString() == "中间节点" ? "" : dtItem.Rows[i]["FORMAT_TYPE_NAME"].ToString();
                        workSheet.Cells[7 + i, 5] = dtItem.Rows[i]["NODE_NAME"].ToString() == "中间节点" ? "" : dtItem.Rows[i]["DATA_TYPE_NAME"].ToString();
                        workSheet.Cells[7 + i, 6] = dtItem.Rows[i]["SINGLE_NAME"].ToString();
                        workSheet.Cells[7 + i, 7] = dtItem.Rows[i]["NODE_NAME"].ToString() == "中间节点" ? "" : dtItem.Rows[i]["UNIT_NAME"].ToString();
                    }

                    //给每个单元格加边框

                    for (int i = 6; i < 7 + row; i++)
                    {
                        for (int j = 1; j < 8; j++)
                        {
                            excelRange = workSheet.get_Range(workSheet.Cells[i, j], workSheet.Cells[i, j]);
                            excelRange.Borders.LineStyle = 1;
                            excelRange.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous,
                        Microsoft.Office.Interop.Excel.XlBorderWeight.xlHairline,
                        Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                        Color.Black.ToArgb());
                        }
                    }

                    //设置边框

                    excelRange = workSheet.get_Range(workSheet.Cells[6, 1], workSheet.Cells[6 + row, 7]);
                    excelRange.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous,
                    Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium,
                    Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic,
                    Color.Black.ToArgb());
                }
                #endregion

                #region 保存

                workBook.Saved = true;
                workBook.SaveCopyAs(excelFileName);
                #endregion

                return true;

            }
            catch
            {
                return false;
            }
            finally
            {
                #region 清空对象并杀掉进程
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
                workSheet = null;
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
                workBook = null;
                excelApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                excelApp = null;
                killAllExcelProcess();
                #endregion
            }

 

   /// <summary>

        /// 杀掉EXCEL进程
        /// </summary>
        protected void killAllExcelProcess()
        {
            System.Diagnostics.Process[] myPs;
            myPs = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process p in myPs)
            {
                if (p.Id != 0)
                {
                    string myS = "EXCEL.EXE" + p.ProcessName + " ID:" + p.Id.ToString();
                    try
                    {
                        if (p.Modules != null)
                            if (p.Modules.Count > 0)
                            {
                                System.Diagnostics.ProcessModule pm = p.Modules[0];
                                myS += "\n Modules[0].FileName:" + pm.FileName;
                                myS += "\n Modules[0].ModuleName:" + pm.ModuleName;
                                myS += "\n Modules[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString();
                                if (pm.ModuleName.ToLower() == "excel.exe")
                                    p.Kill();
                            }
                    }
                    catch
                    { }
                    finally
                    {
                    }
                }
            }
        }

转载于:https://www.cnblogs.com/starzhao/archive/2010/10/25/1860214.html

你可能感兴趣的文章
Jsp抓取页面内容
查看>>
大三上学期软件工程作业之点餐系统(网页版)的一些心得
查看>>
可选参数的函数还可以这样设计!
查看>>
Java语言概述
查看>>
关于BOM知识的整理
查看>>
使用word发布博客
查看>>
面向对象的小demo
查看>>
微服务之初了解(一)
查看>>
GDOI DAY1游记
查看>>
MyBaits动态sql语句
查看>>
HDU4405(期望DP)
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
vs code 的便捷使用
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JS 中的跨域请求
查看>>
JAVA开发环境搭建
查看>>
mysql基础语句
查看>>
Oracle中的rownum不能使用大于>的问题
查看>>
cassandra vs mongo (1)存储引擎
查看>>