简单方法导出EXCLE

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

 

protected void Button1_Click(object sender, EventArgs e)
        {
            DataSet ds= bll.GetAllList();
            DataTable dt=ds.Tables[0];
            OutputExcel(dt, true);
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="dt">要导出的数据</param>
        /// <param name="isTitle">是否显示表头</param>
        public static void OutputExcel(DataTable dt, bool isTitle)
        {
            Excel.ApplicationClass MyExcel = new Excel.ApplicationClass();
            MyExcel.Application.Workbooks.Add(true);
            Excel._Worksheet sheet = (Excel._Worksheet)MyExcel.Workbooks[1].Worksheets[1];
            sheet.Name = "sheet1";
            object Nothing = Missing.Value;
            string filepath = @"D:\我的文档\桌面\123XX.xlsx";// 文件路径及文件名;
           

            try
            {
                if (isTitle)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        MyExcel.Cells[1, i + 1] = dt.Columns[i].ColumnName;//生成表头
                    }
                    MyExcel.get_Range(MyExcel.Cells[1, 1], MyExcel.Cells[1, dt.Columns.Count]).Font.Bold = true;
                    MyExcel.get_Range(MyExcel.Cells[1, 1], MyExcel.Cells[1, dt.Columns.Count]).Font.Size = 12;
                    MyExcel.get_Range(MyExcel.Cells[1, 1], MyExcel.Cells[1, dt.Columns.Count]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; //设置对齐方式
                    MyExcel.get_Range(MyExcel.Cells[1, 1], MyExcel.Cells[1, dt.Columns.Count]).Borders.LineStyle = "1";
                }

                int Rows = 1;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Rows += 1;
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        MyExcel.Cells[Rows, j + 1] = "" + dt.Rows[i][j].ToString();//导入数据
                    }
                    MyExcel.get_Range(MyExcel.Cells[Rows, 1], MyExcel.Cells[Rows, dt.Columns.Count]).Borders.LineStyle = "1";
                }
                sheet.Columns.AutoFit();//自动适应宽度
                MyExcel.Visible = true;
                sheet.SaveAs(filepath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
            }
            catch (Exception) { }
            finally { }
           
           
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值