SpreadsheetControl的Api操作Excel

SpreadsheetControl的Api操作Excel
#region 填充Excel
                    using (var spControl = new SpreadsheetControl())
                    {
                        spControl.LoadDocument(tmpFilename);
                        var wookBook = spControl.Document;
                        var sheet = wookBook.Worksheets[cur.SheetName];
    
                        sheet.Range["A1:AZ60000"].Clear();
                        //填充标题
                        for (int i=0;i<  dt.Columns.Count;i++)
                        {
                            var c=dt.Columns[i];
                            sheet.Cells[0, i].Value = c.ColumnName;
                        }
                        //填充内容
                        for (int i = 1; i < dt.Rows.Count; i++)
                        {
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                if (j == 0)
                                {
                                    sheet.Cells[i, j].Value = dt.Rows[i][j].ToString();
                                }
                                else
                                {
                                    sheet.Cells[i, j].Value =(int) dt.Rows[i][j];
                                }
                            }
                        }
                        spControl.SaveDocument(tmpFilename);

                    }
                    
                    #endregion
View Code

using DevExpress.XtraSpreadsheet.Model;
using DevExpress.XtraSpreadsheet;
using DevExpress.Spreadsheet;

 

SpreadsheetControl的Api操作Excel
            try
            {
                var cur=MainBindingSource.Current as Sys_Report;
                
                if (_SQLEnt == null) throw new Exception("报表必需先绑定查询");
                sysSQLParamsBindingSource.EndEdit();
                var paramList = sysSQLParamsBindingSource.DataSource as List<Sys_SQLParams>;
                var excelTemplateFile = @"c:\123Test.xls";
                var ext = Path.GetExtension(excelTemplateFile);
                var tmpFilename = Path.Combine(Path.GetTempPath(), DateTime.Now.ToString("yyyyMMddhhmmss") + ext);
                Action act = () =>
                {
                    var ds = Fetch<ISys_SQLService>().ExecuteDataSet(_SQLEnt.SQLNo, paramList);
                    var dt=ds.Tables[0];

                    File.Copy(excelTemplateFile, tmpFilename);

                    

                    //ExcelUtil.DataTable2Excel(tmpFilename, dt, cur.SheetName);
      
                    Console.WriteLine(ds);

                };
                InvokeService(act, "执行脚本...");
                Process.Start(tmpFilename);

            }
            catch (Exception ex)
            {
                ErrMsg(ex.Message);
            }
View Code

 

直接拖Spreadsheetcontorl到form上会自动添加程序集的引用

-----------------------------------

生成时图标就没了,这个方案不行。

SpreadsheetControl的Api操作Excel

上一篇:《.NET应用架构设计:原则、模式与实践》新书博客--试读-2.1.2 设计原则实战


下一篇:c# winform程序调试网络请求,记录请求内容到日志