ASP.NETデータリストをExcelにエクスポート

3916 ワード

#region Excel  
                DateTime beforeTime = DateTime.Now;
                //   Excel  (     )
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                if (xlApp == null)
                {
                    ResponseScript("    Excel  ,         Excel");
                    return;
                }
                DateTime afterTime = DateTime.Now;
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//  sheet1
                #endregion

                //    
                long totalCount = _nRecordCount;//   
                int rindex = 0;
                AppCode.Excel.setRangHead(++rindex, "  ", 5, ref worksheet);
                AppCode.Excel.setRangHead(++rindex, "    ", 50, ref worksheet);
                AppCode.Excel.setRangHead(++rindex, "    ", 25, ref worksheet);
                //    
                List<M.VbsCategoryBook> exportlist = new B.VbsCategoryBook().GetList(name, treeid, departCode, order, 1, _nRecordCount, ref _nRecordCount);
                for (int r = 0; r < exportlist.Count; r++)
                {
                    rindex = 0;
                    M.VbsCategoryBook obj = exportlist[r] as M.VbsCategoryBook;
                    AppCode.Excel.setRangCell(r + 2, ++rindex, (r + 1).ToString(), ref worksheet);
                    AppCode.Excel.setRangCell(r + 2, ++rindex, returnCategory(obj.InfoItemid), ref worksheet);
                    AppCode.Excel.setRangCell(r + 2, ++rindex, obj.Name, ref worksheet);
                }

                #region     
                object missing = System.Reflection.Missing.Value;//    missing
                string ExcelFileName = this.Page.Title + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
                //  
                workbook.SaveAs(Server.MapPath("http://www.cnblogs.com/Resources/DownFile/" + ExcelFileName), missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing);
                workbook.Close(null, null, null);
                workbooks.Close();
                xlApp.Application.Quit();
                xlApp.Quit();

                //       
                System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                worksheet = null;
                workbook = null;
                xlApp = null;
                GC.Collect();
                DateTime startTime;
                try
                {
                    //         Excel  
                    System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
                    foreach (System.Diagnostics.Process myProcess in myProcesses)
                    {
                        startTime = myProcess.StartTime;
                        if (startTime >= beforeTime && startTime <= afterTime)
                        {
                            myProcess.Kill();
                        }
                    }
                }
                catch
                {

                }
                //            
                AppCode.Excel.ShowFileInClient(this.Page, Server.MapPath("http://www.cnblogs.com/Resources/DownFile/" + ExcelFileName));
                #endregion