河北三个石头  

首先添加两个引用,如下图:

在引用过程中可能会出现Microsoft.Office 11.0 Object Library引用不上,这个可能是你的Office2003没有选择支持.net导致的,你可以再次运行office的安装文件,然后找到excel项展开节点,选择支持.net

接着在页面里using Namespace:

using Excel = Microsoft.Office.Interop.Excel;

以下是详细代码:

#region Export Data
        /// <summary>
        /// Create By Sw
       /// </summary>
        private void Export()
        {
            Excel.Application excel = new Excel.Application();
            Excel.Workbooks workbooks = excel.Workbooks;
            Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
            Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
            Excel.Range range = null;
            worksheet.Cells[1, 1] = "报关单Excel";
            range = (Excel.Range)worksheet.Cells[1, 1];
            range.Font.Bold = true;
            range.Font.Size = 18;
           //range.Interior.Color = 150; //根据索引设置颜色,索引及值对照参考resource/HelpPic   
            range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//设置文本居中
            range = (Excel.Range)worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 5]);
            range.Select();
            range.Merge(false);

            range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
            string FileName = Server.MapPath("..\\..\\resource\\Templetes\\") + DateTime.Today.ToString("yyyyMMdd") + new Random(DateTime.Now.Millisecond).Next(1000000).ToString() + ".xls";
            
            //Foreach IList,And Add Data To Excel 
            //Write Excel Header
            worksheet.Cells[3, 1] = "报关单号";
            worksheet.Cells[3, 2] = "运输工具";
            worksheet.Cells[3, 3] = "提运单号";
            worksheet.Cells[3, 4] = "核销单号";
            worksheet.Cells[3, 5] = "申报单位";
            worksheet.Cells[3, 6] = "经营单位";
            worksheet.Cells[3, 7] = "收/发货单位";
            worksheet.Cells[3, 8] = "申报日期";
            worksheet.Cells[3, 9] = "处理状态";
            worksheet.Cells[3, 10] = "进口/出口";
            worksheet.Cells[3, 11] = "关区";
            worksheet.Cells[3, 12] = "备注";
            worksheet.Cells[3, 13] = "更新时间";
            //Write Excexl Body
            int flag = 3;
            foreach (var w in lst)
            {
                flag += 1;
                worksheet.Cells[flag, 1] = w.entry_id_new ;
                worksheet.Cells[flag, 2] = w.TRAF_NAME;
                worksheet.Cells[flag, 3] = w.BILL_NO;
                worksheet.Cells[flag, 4] = w.APPR_NO;
                worksheet.Cells[flag, 5] = w.DECL_NAME;
                worksheet.Cells[flag, 6] = w.TRADE_NAME;
                worksheet.Cells[flag, 7] = w.SHIPPER;
                worksheet.Cells[flag, 8] = w.DECL_DATE;
                worksheet.Cells[flag, 9] = w.PROCESS_STATUS;
                worksheet.Cells[flag, 10] = w.EXP_IMP;
                worksheet.Cells[flag, 11] = w.SUB_CUS;
                worksheet.Cells[flag, 12] = w.NOTES;
                worksheet.Cells[flag, 13] = w.UPDATE_TIME;
            }
            try
            {
                workbook.Saved = true;
                workbook.SaveCopyAs(FileName);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            workbooks.Close();
            if (excel != null)
            {
                excel.Workbooks.Close();
                excel.Quit();
                int Generation = System.GC.GetGeneration(excel);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                excel = null;
                GC.Collect(Generation);
            }
            GC.Collect();//强行销毁  

            //关闭最近打开的Excel进程
            System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
            System.DateTime startTime = new DateTime();
            int m, killId = 0;
            for (m = 0; m < excelProc.Length; m++)
            {
                if (startTime < excelProc[m].StartTime)
                {
                    startTime = excelProc[m].StartTime;
                    killId = m;
                }
            }
            if (excelProc[killId].HasExited == false)
            {
                excelProc[killId].Kill();
            }
            //提供下载   
            System.IO.FileInfo file = new System.IO.FileInfo(FileName);
            Response.Clear();
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            // 添加头信息,为"文件下载/另存为"对话框指定默认文件名    
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
            // 添加头信息,指定文件大小,让浏览器能够显示下载进度    
            Response.AddHeader("Content-Length", file.Length.ToString());
            // 指定返回的是一个不能被客户端读取的流,必须被下载    
            Response.ContentType = "Application/ms-excel";
            // 把文件流发送到客户端    
            Response.WriteFile(file.FullName);
        }
        #endregion

posted on 2013-06-08 14:28  河北三个石头  阅读(168)  评论(0编辑  收藏  举报