[英]How do I export 2 gridviews into 2 separate sheets in one MS Excel file?
如何通過單擊1按鈕將2個gridviews(GridView1和GridView2)導出到一個MS Excel文件中的2個單獨的工作表中? 目前,我只能將1個gridview導出到Excel工作表,該工作表的文件名與工作表名稱相同。 但是我想將gridviews分為2個單獨的工作表,我希望工作表名稱由我自己定義/設置。 謝謝
public void ExportGridToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName ="Export"+DateTime.Now+".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
GridView1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();
}
//編輯我的答案,因為只有一個導出按鈕:看一下這個問題 :
首先添加以下名稱空間:
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
因此,基本上,您應該擁有(或繼續制作)2個DataTable(以綁定到2個gridviews):dt1和dt2
制作數據集並向其中添加2個數據表
DataSet dataset = new DataSet();
dataset.Tables.Add(dt1);
dataset.Tables.Add(dt2);
然后
//Print using Ofice InterOp
Excel.Application excel = new Excel.Application();
var workbook = (Excel._Workbook)(excel.Workbooks.Add(Missing.Value));
for (var i = 0; i < dataset.Tables.Count; i++)
{
if (workbook.Sheets.Count <= i)
{
workbook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
}
//NOTE: Excel numbering goes from 1 to n
var currentSheet = (Excel._Worksheet)workbook.Sheets[i + 1];
for (var y = 0; y < dataset.Tables[i].Rows.Count; y++)
{
for (var x = 0; x < dataset.Tables[i].Rows[y].ItemArray.Count(); x++)
{
currentSheet.Cells[y+1, x+1] = dataset.Tables[i].Rows[y].ItemArray[x];
}
}
}
string outfile = @"C:\APP_OUTPUT\EXCEL_TEST.xlsx";
workbook.SaveAs( outfile, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
workbook.Close();
excel.Quit();
更改工作表名稱,我相信您可以這樣做:
currentSheet.Name = "your sheet name"
您需要修改代碼,以便在excel文件中創建工作表,然后將數據從網格手動復制到excel文件,並將此代碼創建為以Grid,Sheetname和Sheetid之類的參數(如1,2等)作為參數的函數然后在按鈕單擊事件中,分別對grid1調用兩次,對grid2調用一次,分別使用不同的工作表名稱和工作表ID 1和2對其進行調用。 該函數的代碼如下。
在按鈕單擊事件中編寫這兩行代碼,然后通過將excel應用程序和工作簿也作為參數傳遞給每個網格兩次調用該函數。
// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook,GridView gridview,string SheetName,int sheetid)
{
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
app.Visible = true;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet"+ sheetid];
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = sheetname;
// storing header part in Excel
for(int i=1;i<gridview.Columns.Count+1;i++)
{
worksheet.Cells[1, i] = gridview.Columns[i-1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i=0; i < gridview.Rows.Count-1 ; i++)
{
for(int j=0;j<gridview.Columns.Count;j++)
{
worksheet.Cells[i + 2, j + 1] = gridview.Rows[i].Cells[j].Value.ToString();
}
}
// save the application
workbook.SaveAs("c:\\output.xls",Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive , Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Exit from the application
app.Quit();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.