一段实现将数据导出到EXCEL中的代码,不完整,,求补充!
以下这段是将GirdView的数据导出到Excel的代码,,在网上查到的。初学,不知道前后应如何补充使其完整,,求高手帮我补充一下。 this.GridView1.DataBind();
Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
object oMissing = System.Reflection.Missing.Value;
oExcel.Workbooks.Add(oMissing);
Excel.Workbook oBook = oExcel.Workbooks[1];
Excel.Worksheet oSheet = (Excel.Worksheet)oBook.Sheets[1];
oSheet.Name = this.Title;
Excel.Range rg;
for (int j = 0; j < this.GridView1.HeaderRow.Cells.Count; j++)
{
rg = ((Excel.Range)oSheet.Cells[ 1, j + 1]);
rg.FormulaR1C1 = this.GridView1.HeaderRow.Cells[j].Text;
}
for(int i = 0;i<this.GridView1.Rows.Count;i++)
{
for (int j = 0; j < this.GridView1.Rows[0].Cells.Count; j++)
{
rg = ((Excel.Range)oSheet.Cells[i + 2, j+1]);
rg.FormulaR1C1 = this.GridView1.Rows[i].Cells[j].Text;
}
}
rg = null;
string VirFileName = Guid.NewGuid().ToString() + ".xls";
oBook.SaveAs(Server.MapPath(VirFileName), Excel.XlFileFormat.xlExcel9795, oMissing, oMissing, oMissing, oMissing, Excel.XlSaveAsAccessMode.xlExclusive,
oMissing, oMissing, oMissing, oMissing, oMissing);
oExcel.Workbooks.Close();
oExcel.Quit();
oSheet = null;
oBook = null;
oExcel = null;
GC.Collect();
Response.Redirect(VirFileName);