using System.Runtime.InteropServices;
using excel = Microsoft.Office.Interop.Excel;
private void ExporttoExcel()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
Microsoft.Office.Interop.Excel.Application oXL;
Microsoft.Office.Interop.Excel.Workbook oWB;
Microsoft.Office.Interop.Excel.Worksheet oSheet;
Microsoft.Office.Interop.Excel.Range oRange;
// Start Excel and get Application object.
oXL = new Microsoft.Office.Interop.Excel.Application();
// Set some properties
oXL.Visible = true;
oXL.DisplayAlerts = false;
// Get a new workbook.
oWB = oXL.Workbooks.Add(Missing.Value);
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "Correspondence History";
string[] Cellsnames = { "a", "b", "c", "d", "e", "f", "g", "h" };
if (gvReportview != null)
{
if (gvReportview.Rows.Count > 0)
{
for (int j = 0; j < gvReportview.HeaderRow.Cells.Count; j++)
{
oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString());
oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString()).EntireRow.Font.Bold = true;
oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString()).EntireRow.Font.Size = 10;
oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString()).EntireRow.Font.Name = "arial"; oRange.Borders.Color = System.Drawing.Color.Black.ToArgb();
// oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString()).Interior.Pattern = Microsoft.Office.Interop.Excel.XlPattern.xlPatternGray50;
oRange.EntireColumn.ColumnWidth = 20; oRange.EntireColumn.RowHeight = 15;
oRange.EntireColumn.WrapText = true;
oRange = oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString());
oRange.FormulaR1C1 = gvReportview.HeaderRow.Cells[j].Text;
oRange.HorizontalAlignment = 3;
oRange.VerticalAlignment = 3;
}
int k = 0; int m = 0;
foreach (GridViewRow gvrow in gvReportview.Rows)
{
k = m;
for (int i = 0; i < gvrow.Cells.Count; i++)
{
oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString());
//oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString()).EntireRow.Font.Bold = true;
oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString()).EntireRow.Font.Size = 10;
oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString()).EntireRow.Font.Name = "arial";
oRange.Borders.Color = System.Drawing.Color.Black.ToArgb();
// oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString()).Interior.Pattern = Microsoft.Office.Interop.Excel.XlPattern.xlPatternGray50;
oRange.EntireColumn.ColumnWidth = 20; oRange.EntireColumn.RowHeight = 15;
oRange.EntireColumn.WrapText = true;
oRange = oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString());
string CellText = string.Empty;
if (i == 0)
{
Label lblreferenceno = (Label)gvrow.FindControl("lblreferenceno");
if (!string.IsNullOrEmpty(lblreferenceno.Text))
CellText = lblreferenceno.Text;
else
{
HyperLink lnkview = (HyperLink)gvrow.FindControl("lnkview");
CellText = lnkview.Text;
}
}
else if (i == 1)
{
Label lblreferenceno = (Label)gvrow.FindControl("lbltitle");
if (!string.IsNullOrEmpty(lblreferenceno.Text))
CellText = lblreferenceno.Text;
}
else if (i == 2)
{
Label lblreferenceno = (Label)gvrow.FindControl("lblsender");
if (!string.IsNullOrEmpty(lblreferenceno.Text))
CellText = lblreferenceno.Text;
}
else if (i == 3)
{
Label lblreferenceno = (Label)gvrow.FindControl("lblreceiveddate");
if (!string.IsNullOrEmpty(lblreferenceno.Text))
CellText = lblreferenceno.Text;
}
else if (i == 4)
{
Label lblreferenceno = (Label)gvrow.FindControl("lblsubject");
if (!string.IsNullOrEmpty(lblreferenceno.Text))
CellText = lblreferenceno.Text;
}
//else if (i == 5)
//{
// Label lblreferenceno = (Label)gvrow.FindControl("lblreferenceno");
// if (!string.IsNullOrEmpty(lblreferenceno.Text))
// CellText = lblreferenceno.Text;
//}
oRange.FormulaR1C1 = CellText;//Adding datat to cell
oRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
oRange.VerticalAlignment = 3;
}
m = m + 1;
k = m;
}
}
}
//oSheet.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA4;
//oSheet.PageSetup.PrintGridlines = true;
oSheet = null;
oRange = null;
string temppath = System.IO.Path.GetTempPath();
if (File.Exists(temppath + "Correspondence Report.xlsx"))
File.Delete(temppath + "Correspondence Report.xlsx");
oXL.ActiveWorkbook.SaveCopyAs(temppath + "Correspondence Report.xlsx");
oWB.Close(Missing.Value, Missing.Value, Missing.Value);
oWB = null;
oXL.Quit();
releaseObject(oSheet);
releaseObject(oWB);
releaseObject(oXL);
ShowExcel(temppath + "Correspondence Report.xlsx");
if (File.Exists(temppath + "Correspondence Report.xlsx"))
File.Delete(temppath + "Correspondence Report.xlsx");
}
catch (Exception ex)
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = ex.Message.ToString();
}
});
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
// MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
private void ShowExcel(string strS)
{
try
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment; filename=" + strS);
//Response.AddHeader("Content-Disposition", "attachment; filename=ravi.xls");
Response.TransmitFile(strS);
Response.End();
//Response.WriteFile(strS);
Response.Flush();
Response.Clear();
}
catch (Exception ex)
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = ex.Message.ToString();
}
}
using excel = Microsoft.Office.Interop.Excel;
private void ExporttoExcel()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
Microsoft.Office.Interop.Excel.Application oXL;
Microsoft.Office.Interop.Excel.Workbook oWB;
Microsoft.Office.Interop.Excel.Worksheet oSheet;
Microsoft.Office.Interop.Excel.Range oRange;
// Start Excel and get Application object.
oXL = new Microsoft.Office.Interop.Excel.Application();
// Set some properties
oXL.Visible = true;
oXL.DisplayAlerts = false;
// Get a new workbook.
oWB = oXL.Workbooks.Add(Missing.Value);
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "Correspondence History";
string[] Cellsnames = { "a", "b", "c", "d", "e", "f", "g", "h" };
if (gvReportview != null)
{
if (gvReportview.Rows.Count > 0)
{
for (int j = 0; j < gvReportview.HeaderRow.Cells.Count; j++)
{
oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString());
oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString()).EntireRow.Font.Bold = true;
oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString()).EntireRow.Font.Size = 10;
oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString()).EntireRow.Font.Name = "arial"; oRange.Borders.Color = System.Drawing.Color.Black.ToArgb();
// oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString()).Interior.Pattern = Microsoft.Office.Interop.Excel.XlPattern.xlPatternGray50;
oRange.EntireColumn.ColumnWidth = 20; oRange.EntireColumn.RowHeight = 15;
oRange.EntireColumn.WrapText = true;
oRange = oSheet.get_Range(Cellsnames[j] + (1).ToString(), Cellsnames[j] + (1).ToString());
oRange.FormulaR1C1 = gvReportview.HeaderRow.Cells[j].Text;
oRange.HorizontalAlignment = 3;
oRange.VerticalAlignment = 3;
}
int k = 0; int m = 0;
foreach (GridViewRow gvrow in gvReportview.Rows)
{
k = m;
for (int i = 0; i < gvrow.Cells.Count; i++)
{
oRange = (Microsoft.Office.Interop.Excel.Range)oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString());
//oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString()).EntireRow.Font.Bold = true;
oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString()).EntireRow.Font.Size = 10;
oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString()).EntireRow.Font.Name = "arial";
oRange.Borders.Color = System.Drawing.Color.Black.ToArgb();
// oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString()).Interior.Pattern = Microsoft.Office.Interop.Excel.XlPattern.xlPatternGray50;
oRange.EntireColumn.ColumnWidth = 20; oRange.EntireColumn.RowHeight = 15;
oRange.EntireColumn.WrapText = true;
oRange = oSheet.get_Range(Cellsnames[i] + (k + 2).ToString(), Cellsnames[i] + (k + 2).ToString());
string CellText = string.Empty;
if (i == 0)
{
Label lblreferenceno = (Label)gvrow.FindControl("lblreferenceno");
if (!string.IsNullOrEmpty(lblreferenceno.Text))
CellText = lblreferenceno.Text;
else
{
HyperLink lnkview = (HyperLink)gvrow.FindControl("lnkview");
CellText = lnkview.Text;
}
}
else if (i == 1)
{
Label lblreferenceno = (Label)gvrow.FindControl("lbltitle");
if (!string.IsNullOrEmpty(lblreferenceno.Text))
CellText = lblreferenceno.Text;
}
else if (i == 2)
{
Label lblreferenceno = (Label)gvrow.FindControl("lblsender");
if (!string.IsNullOrEmpty(lblreferenceno.Text))
CellText = lblreferenceno.Text;
}
else if (i == 3)
{
Label lblreferenceno = (Label)gvrow.FindControl("lblreceiveddate");
if (!string.IsNullOrEmpty(lblreferenceno.Text))
CellText = lblreferenceno.Text;
}
else if (i == 4)
{
Label lblreferenceno = (Label)gvrow.FindControl("lblsubject");
if (!string.IsNullOrEmpty(lblreferenceno.Text))
CellText = lblreferenceno.Text;
}
//else if (i == 5)
//{
// Label lblreferenceno = (Label)gvrow.FindControl("lblreferenceno");
// if (!string.IsNullOrEmpty(lblreferenceno.Text))
// CellText = lblreferenceno.Text;
//}
oRange.FormulaR1C1 = CellText;//Adding datat to cell
oRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
oRange.VerticalAlignment = 3;
}
m = m + 1;
k = m;
}
}
}
//oSheet.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA4;
//oSheet.PageSetup.PrintGridlines = true;
oSheet = null;
oRange = null;
string temppath = System.IO.Path.GetTempPath();
if (File.Exists(temppath + "Correspondence Report.xlsx"))
File.Delete(temppath + "Correspondence Report.xlsx");
oXL.ActiveWorkbook.SaveCopyAs(temppath + "Correspondence Report.xlsx");
oWB.Close(Missing.Value, Missing.Value, Missing.Value);
oWB = null;
oXL.Quit();
releaseObject(oSheet);
releaseObject(oWB);
releaseObject(oXL);
ShowExcel(temppath + "Correspondence Report.xlsx");
if (File.Exists(temppath + "Correspondence Report.xlsx"))
File.Delete(temppath + "Correspondence Report.xlsx");
}
catch (Exception ex)
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = ex.Message.ToString();
}
});
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
// MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
private void ShowExcel(string strS)
{
try
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment; filename=" + strS);
//Response.AddHeader("Content-Disposition", "attachment; filename=ravi.xls");
Response.TransmitFile(strS);
Response.End();
//Response.WriteFile(strS);
Response.Flush();
Response.Clear();
}
catch (Exception ex)
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = ex.Message.ToString();
}
}
No comments:
Post a Comment