【问题标题】:How to Export Webcontrol table to excel in asp.net mvc如何在asp.net mvc中将Webcontrol表导出到excel
【发布时间】:2017-12-08 14:15:21
【问题描述】:
List<Table> t = (List<Table>)GenerateLayout(dtDPList, dtBOList, dpl);

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

【问题讨论】:

  • 固定代码缩进。

标签: web-controls


【解决方案1】:

您可以使用以下通用方法将数据表转换为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

您可以使用以下链接找到更多样式标签:

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

【讨论】:

  • for (int i = 0; i
  • 大哥,样式怎么应用??我使用 xlWorkSheet.Rows[i].Style = t[0].Rows[i].Style;在 for 循环中,但它显示错误,所以请帮助我..
  • 您需要创建一个样式对象,然后需要定义样式属性。然后您可以将样式对象分配给工作簿。请查看更新答案。希望对您有所帮助。
  • 您可以在-techbrij.com/export-excel-xls-xlsx-asp-net-npoi-epplus中找到更多详细信息
【解决方案2】:

列表列表 = 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多