Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Excel Script

This code is exporting data from a data grid view to an Excel spreadsheet. It loops through the columns and rows of the data grid, writes the header text and values to the spreadsheet. It then formats the header by merging and styling cells, adds a print date, adjusts column widths, and saves the file, displaying a success or failure message.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Excel Script

This code is exporting data from a data grid view to an Excel spreadsheet. It loops through the columns and rows of the data grid, writes the header text and values to the spreadsheet. It then formats the header by merging and styling cells, adds a print date, adjusts column widths, and saves the file, displaying a success or failure message.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

worksheet.Cells[4, i] = DTGRIDVIEWEXCEL.Columns[i - 1].

HeaderText;
worksheet.Cells[4, i].EntireRow.Font.Bold = true;

}
// storing Each row and column value to excel sheet
for (int i = 0; i < DTGRIDVIEWEXCEL.Rows.Count; i++)
{
for (int j = 0; j < DTGRIDVIEWEXCEL.Columns.Count; j++)
{
if (tabControl1.SelectedTab == tabPage1)
{

worksheet.Cells[i + 5, j + 1] =
DTGRIDVIEWEXCEL.Rows[i].Cells[j].Value.ToString();

}
}
worksheet.Columns.AutoFit();

int lastUsedColumn = worksheet.Cells.Find("*",


System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
Excel.XlSearchOrder.xlByColumns,
Excel.XlSearchDirection.xlPrevious,
false, System.Reflection.Missing.Value,
System.Reflection.Missing.Value).Column;

// Code For Header

worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[2,


lastUsedColumn]].Merge();

worksheet.Cells[1, 1] = "OPTECH REPORT";


worksheet.Cells[1, 1].Font.Bold = true;
worksheet.Cells[1, 1].Font.Size = 18;
worksheet.Cells[1, 1].HorizontalAlignment =
Excel.XlHAlign.xlHAlignCenter;
worksheet.Cells[1, 1].VerticalAlignment =
Excel.XlVAlign.xlVAlignCenter;
worksheet.Cells[1, 1].Font.Color = Excel.XlRgbColor.rgbDarkRed;

worksheet.Cells[1, 1].Borders.LineStyle =
Excel.XlLineStyle.xlContinuous;

//Code for Print Date


worksheet.Range[worksheet.Cells[3, 1], worksheet.Cells[3,
lastUsedColumn]].Merge();
worksheet.Cells[3, 1] = DateTime.Now.ToString();
worksheet.Cells[3, 1].Font.Bold = true;
worksheet.Cells[3, 1].Font.Size = 14;
worksheet.Cells[3, 1].HorizontalAlignment =
Excel.XlHAlign.xlHAlignCenter;
worksheet.Cells[3, 1].VerticalAlignment =
Excel.XlVAlign.xlVAlignCenter;
worksheet.Cells[3, 1].Font.Color = Excel.XlRgbColor.rgbDarkBlue;
for (int i = 1; i <= lastUsedColumn; i++)
{
if (worksheet.Columns[i].ColumnWidth < 10)
{
worksheet.Columns[i].ColumnWidth = 10;
}
}

SaveFileDialog1.ShowDialog();
try
{
workbook.SaveAs(SaveFileDialog1.FileName, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing,
Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
this.Cursor = Cursors.Default;
app.Visible = true;
MessageBox.Show("Excel Export Successful");
}
catch (Exception)
{

MessageBox.Show("Excel Export Failed");


}

// Exit from the application


//app.Quit();

You might also like