当前位置:软件学习 > Excel >>

.Net中Com组件导出Excel

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

在引用过程中可能会出现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)
            {
              

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,