0% found this document useful (0 votes)
76 views3 pages

Excel Script

This code contains two sections: 1. An animation loop that resizes a window over 300 iterations in increments of 30 pixels. 2. Code to export data from a datagridview to an Excel spreadsheet. It loops through the rows and columns, writes the header and values to a new worksheet, formats cells, autosizes columns, and saves the file based on a save file dialog box selection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views3 pages

Excel Script

This code contains two sections: 1. An animation loop that resizes a window over 300 iterations in increments of 30 pixels. 2. Code to export data from a datagridview to an Excel spreadsheet. It loops through the rows and columns, writes the header and values to a new worksheet, formats cells, autosizes columns, and saves the file based on a save file dialog box selection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// code for Animation in Open

for (int i = 0; i < 300; i++)


{
Thread.Sleep(10);
Rectangle screen = Screen.FromControl(this).Bounds;
this.ClientSize = new System.Drawing.Size(screen.Width, i);
//a = a + 30;
}

{
Thread.Sleep(1);
Rectangle screen = Screen.FromControl(this).Bounds;
this.ClientSize = new System.Drawing.Size(screen.Width, i);
//a = a + 30;
}
base.OnDeactivate(e);
this.Close();

// Code for Excel File report print


using Excel = Microsoft.Office.Interop.Excel;

private void btnExport_Click()


{

DataGridView DTGRIDVIEWEXCEL = new DataGridView();

DTGRIDVIEWEXCEL = dataGridView1;

// creating Excel Application


Excel._Application app = new
Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
app.Visible = false;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";

// storing header part in Excel


for (int i = 1; i < DTGRIDVIEWEXCEL.Columns.Count + 1; i++)
{
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy