using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Excel = Microsoft.Office.Interop.Excel;
namespace FleetManagment
{
public partial class ShowBranches : Form
{
string value1;
string value2;
string value3;
string value4;
DataView dv;
public ShowBranches(string str1, string str2, string str3, string str4)
{
InitializeComponent();
value1 = str1;
value2 = str2;
value3 = str3;
value4 = str4;
}
private void ShowBranches_Load(object sender, EventArgs e)
{
label1.Text = Fleet.Branches;
int a;
FleetManagment.Fleet fleet = new FleetManagment.Fleet();
DataTable dt = fleet.ViewBranchesID("Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4, Convert.ToInt32(label1.Text));
dv = dt.DefaultView;
if (dt.Rows.Count > 0)
{
a = Convert.ToInt32(dt.Rows.Count);
label1.Text = a.ToString();
dataGridView1.DataSource = dv;
dataGridView1.Refresh();
}
else
{
MessageBox.Show("No Records Found");
}
}
private void button1_Click(object sender, EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
e.Graphics.DrawImage(bm, 150, 150, 450, 350);
}
private void button2_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
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
//Funny
app.Visible = true;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
try
{
//Fixed:(Microsoft.Office.Interop.Excel.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 Ketoan";
// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
// save the application
string fileName = String.Empty;
//SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Excel files |*.xlsx|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = saveFileDialog1.FileName;
//Fixed-old code :11 para->add 1:Type.Missing
workbook.SaveAs(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, Type.Missing);
}
else
return;
// Exit from the application
//app.Quit();
}
catch (System.Exception ex)
{
MessageBox.Show("The error is " + ex);
}
finally
{
app.Quit();
workbook = null;
app = null;
}
}
}
}
This above my code all lines with practise and without comments line
line 84 without comment line is
saveFileDialog1.RestoreDirectory = true;
Ahmedsa, if this helps please login to Mark As Answer. | Alert Moderator