在C#中导出数据到Excel

在C#中对Excel的操作是非常方便的,写好一个Excel的模板文件,在程序中对模板就行操作。示例代码如下:

	/// <summary>
        /// 生成Excel文件
        /// </summary>
        public void HinBanInfoXmlFile()
        {
            //模版路径
            string tempFilePath = AppDomain.CurrentDomain.BaseDirectory + "Model\\Template\\AA.xlt";

            // Excel生成
            ApplicationClass app = new ApplicationClass();
            app.DisplayAlerts = false;

            try
            {
                // 打开创建文件
                Workbook book = app.Workbooks.Open(tempFilePath, Missing.Value, true, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                Worksheet theWorkSheet = (Worksheet)book.ActiveSheet;

                //设置头部信息
                theWorkSheet.get_Range("MakeDate", Missing.Value).Value2 = DateTime.Now;
                theWorkSheet.get_Range("MakeTime", Missing.Value).Value2 = DateTime.Now;
                theWorkSheet.get_Range("ChargeId", Missing.Value).Value2 = loginEmp.empCode;
                theWorkSheet.get_Range("ChargeName", Missing.Value).Value2 = loginEmp.empName;

                int iRow = 7; // 第一行为标题,从第二行开始写入
                for (int i = 0; i < this.dataList.Length; i++)
                {
                    HinBanInfo model = dataList[i]; // 行数据
                    if (i < this.dataList.Length - 1)
                    {
                        ((Range)theWorkSheet.Rows.get_Item(iRow, Missing.Value)).Copy(Missing.Value);
                        ((Range)theWorkSheet.Rows.get_Item(iRow, Missing.Value)).Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
                    }
                    //对单元格设置显示的数据
                    theWorkSheet.get_Range("A" + iRow, Missing.Value).Value2 = model.flag;
                                  
                   iRow++;
                    //写进度文件
                    ProgressWatch.writeProgress(fileName, i * 100 / dataList.Length, ProgressStatus.Running);
                }

                /// <summary>
                /// Excel格式保存
                /// </summary>
                book.SaveAs(fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, Missing.Value, Missing.Value, Missing.Value
                    , Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                // 完成后关闭文件
                app.ActiveWorkbook.Close(false, null, null);
                // 如果停止删除文件,否则进度完成
                if (ProgressWatch.isStop(fileName))
                    ProgressWatch.deleteServerFile(fileName);
                else
                    ProgressWatch.writeProgress(fileName, 100, ProgressStatus.Complete);
            }
            catch (Exception e)
            {
                //关闭
                if (app != null && app.ActiveWorkbook != null)
                    app.ActiveWorkbook.Close(false, null, null);
                ProgressWatch.writeProgress(fileName, 0, ProgressStatus.Stop);
                msgLog = msgLog + "->失败!" + e.Message;
            }
            finally
            {
                // 写日志
                Log.write(logType, msgLog, loginEmp);
                // 安全退出
                app.Quit();
                app = null;
            }
        }
在模板中可以对单元格的样式进行修改。

上一篇:使用Mac安装东西报checkPermissions Missing write access to /usr/local/lib/node_modules


下一篇:android简单登陆和注册功能实现+SQLite数据库学习