List<Table> t = (List<Table>)GenerateLayout(dtDPList, dtBOList, dpl);
在列表中我得到了3个Webcontrol表,我想将这些webconrol表导出为excel ..所以请帮助我..
答案 0 :(得分:0)
您可以使用以下通用方法将datatable转换为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
答案 1 :(得分:0)
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();