public void ExporExcelusingOffice()
{
// 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);
// 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 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < grdViewYMP.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = grdViewYMP.HeaderRow.Cells[i - 1].Text.Trim();
}
DataTable dtYMP = ViewState["dtYMP"] as DataTable;
// storing Each row and column value to excel sheet
for (int i = 0; i < grdViewYMP.Rows.Count - 1; i++)
{
for (int j = 0; j < grdViewYMP.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dtYMP.Rows[i][j].ToString();
//worksheet.Cells[i + 2, j + 1] = grdViewYMP.Rows[i].Cells[j].Text;
}
}
// save the application
object misValue = System.Reflection.Missing.Value;
// 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);
workbook.SaveAs("c:\\YMPoutput.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
// Exit from the application
app.Quit();
}
{
// 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);
// 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 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < grdViewYMP.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = grdViewYMP.HeaderRow.Cells[i - 1].Text.Trim();
}
DataTable dtYMP = ViewState["dtYMP"] as DataTable;
// storing Each row and column value to excel sheet
for (int i = 0; i < grdViewYMP.Rows.Count - 1; i++)
{
for (int j = 0; j < grdViewYMP.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dtYMP.Rows[i][j].ToString();
//worksheet.Cells[i + 2, j + 1] = grdViewYMP.Rows[i].Cells[j].Text;
}
}
// save the application
object misValue = System.Reflection.Missing.Value;
// 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);
workbook.SaveAs("c:\\YMPoutput.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
// Exit from the application
app.Quit();
}



No comments:
Post a Comment