如何在 asp.net mvc 中将 Webcontrol table 导出到 excel

How to Export Webcontrol table to excel in asp.net mvc

List<Table> t = (List<Table>)GenerateLayout(dtDPList, dtBOList, dpl);

在列表中我得到了 3 Webcontrol table,我想将那些 webconrol tables 导出到 excel..所以请帮助我..

您可以使用以下通用方法将数据表转换为 excel:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelDemo
{
    public class ExcelUtility
    {
    public static void CreateExcel(DataSet ds, string excelPath)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        try
        {
            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    xlWorkSheet.Cells[i + 1, j + 1] = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                }
            }

            xlWorkBook.SaveAs(excelPath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlApp);
            releaseObject(xlWorkBook);
            releaseObject(xlWorkSheet);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private static void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch
        {
            obj = null;
        }
        finally
        {
            GC.Collect();
        }
    } 
}

}

您可以直接将样式属性应用于单元格,如下所示:

workSheet.Cells[2, 1].Font.Bold = true

您可以使用下面的 link 找到更多样式标签:

http://csharp.net-informations.com/excel/csharp-format-excel.htm

List list = GenerateLayout(dt1,dt2,dpl); Response.ClearContent(); Response.AddHeader("content-disposition", "attachment;filename=DeliveryPickList.xls"); Response.AddHeader("Content-Type", "application/vnd.ms-excel");

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                list[0].RenderControl(htw);
                TextWriter output= Response.Output;
                output.Write(sw.ToString());
            }
        }
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                list[1].RenderControl(htw);
                TextWriter output = Response.Output;
                output.Write(sw.ToString());
            }
        }
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                list[2].RenderControl(htw);
                TextWriter output = Response.Output;
                output.Write(sw.ToString());
            }
        }
        Response.End();