遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

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;

 

            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上会自动添加程序集的引用

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

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

参考DevExpress带的Demo

posted on 2021-09-04 09:38  遗忘海岸  阅读(587)  评论(0编辑  收藏  举报