对 Aspose从DataGrid导出 的封装
/// <summary>
/// 从DataGrid导出
/// </summary>
/// <param name="dg">源数据网格</param>
/// <param name="m">最大行</param>
/// <param name="n">最大列</param>
/// <param name="header">输出到Excel中的表头[需隐藏的列的列头也需设置,以占位]。</param>
/// <param name="hideColumnIndex">需隐藏的列的索引[从0开始]</param>
public void ExportExcelByDataGrid(System.Web.UI.WebControls.DataGrid dg,int m, int n ,string[] header,params byte[] hideColumnIndex)
{
//替换
for(int i = 0;i < dg.Items.Count;i++)
{
for(int j = 0;j < dg.Columns.Count;j++)
{
if(dg.Items[i].Cells[j].Text == " ")
{
dg.Items[i].Cells[j].Text = "";
}
}
}
Excel excel = new Excel();
//Excel.SetLicense(SystemConfig.GetCfgStr("AposeExcelLicense"),(System.Web.UI.Page)System.Web.HttpContext.Current.Handler);
Worksheet sheet = excel.Worksheets[0];
sheet.Cells.ImportDataGrid(dg, 0, 0, m, n, true);
foreach(byte index in hideColumnIndex)
{
sheet.Cells.HideColumn(index);
}
//将表头加入
sheet.Cells.InsertRow(0);
for(byte i = 0;i < header.Length;i++)
{
sheet.Cells[0,i].PutValue(header[i]);
}
sheet.Name = "Sheet1";
excel.Save("export.xls", SaveType.OpenInBrowser, FileFormatType.Default,HttpContext.Current.Response);
}
/// 从DataGrid导出
/// </summary>
/// <param name="dg">源数据网格</param>
/// <param name="m">最大行</param>
/// <param name="n">最大列</param>
/// <param name="header">输出到Excel中的表头[需隐藏的列的列头也需设置,以占位]。</param>
/// <param name="hideColumnIndex">需隐藏的列的索引[从0开始]</param>
public void ExportExcelByDataGrid(System.Web.UI.WebControls.DataGrid dg,int m, int n ,string[] header,params byte[] hideColumnIndex)
{
//替换
for(int i = 0;i < dg.Items.Count;i++)
{
for(int j = 0;j < dg.Columns.Count;j++)
{
if(dg.Items[i].Cells[j].Text == " ")
{
dg.Items[i].Cells[j].Text = "";
}
}
}
Excel excel = new Excel();
//Excel.SetLicense(SystemConfig.GetCfgStr("AposeExcelLicense"),(System.Web.UI.Page)System.Web.HttpContext.Current.Handler);
Worksheet sheet = excel.Worksheets[0];
sheet.Cells.ImportDataGrid(dg, 0, 0, m, n, true);
foreach(byte index in hideColumnIndex)
{
sheet.Cells.HideColumn(index);
}
//将表头加入
sheet.Cells.InsertRow(0);
for(byte i = 0;i < header.Length;i++)
{
sheet.Cells[0,i].PutValue(header[i]);
}
sheet.Name = "Sheet1";
excel.Save("export.xls", SaveType.OpenInBrowser, FileFormatType.Default,HttpContext.Current.Response);
}