Step 1:
First, you need to add Microsoft.Office.Interop.Excel as a reference
Step 2:
private void GridDesign()
{
dgvAdd.Columns.Add("docno", "Doc_No");
dgvAdd.Columns.Add("cname", "Consignee");
dgvAdd.Columns.Add("phone", "Phone_No");
dgvAdd.Columns.Add("pin", "Pin_Code");
}
Step 3:
namespace Data_Entry_Application
{
public partial class Report : Form
{
string uid = "";
int srows = 0;
string conn = ConfigurationSettings.AppSettings["constr"];
public Report()
{
InitializeComponent();
GridDesign();
}
Step 4:
Convert Excel Button Coding
private void butExcel_Click(object sender, EventArgs e)
{
dgvAdd.Rows.Clear();
SqlConnection con = new SqlConnection(conn);
con.Open();
if (radioAll.Checked == true)
{
string fdate = "";
string tdate = "";
string find = "";
if (radioPall.Checked == true)
{
find = "select DocNo,Consignee,PhoneNo,Pincode from BluStar_Details order by Pincode";
}
else if (radioPdate.Checked == true)
{
int year1 = dtpdate1.Value.Date.Year;
int month1 = dtpdate1.Value.Date.Month;
int day1 = dtpdate1.Value.Date.Day;
fdate = datetimeconvert(year1, month1, day1);
int year2 = dtpDate2.Value.Date.Year;
int month2 = dtpDate2.Value.Date.Month;
int day2 = dtpDate2.Value.Date.Day;
tdate = datetimeconvert(year2, month2, day2);
find = "select DocNo,Consignee,PhoneNo,Pincode from BluStar_Details where (DocDate BETWEEN '" + fdate + "' AND '" + tdate + "') order by Pincode";
}
SqlCommand cmd1234 = new SqlCommand(find, con);
SqlDataReader dr = cmd1234.ExecuteReader();
while (dr.Read())
{
string val = dr["DocNo"].ToString();
string val1 = dr["Consignee"].ToString();
string val2 = dr["PhoneNo"].ToString();
string val3 = dr["Pincode"].ToString();
dgvAdd.Rows.Add(val, val1, val2, val3);
}
dr.Close();
if (dgvAdd.Rows.Count > 0)
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = true;
try
{
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
worksheet.Name = "Exported from DataGridView";
for (int i = 1; i < dgvAdd.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dgvAdd.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dgvAdd.Rows.Count - 1; i++)
{
for (int j = 0; j < dgvAdd.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dgvAdd.Rows[i].Cells[j].Value.ToString();
}
}
string fileName = String.Empty;
saveFileExcel.Filter = "Excel files |*.xls|All files (*.*)|*.*";
saveFileExcel.FilterIndex = 2;
saveFileExcel.RestoreDirectory = true;
if (saveFileExcel.ShowDialog() == DialogResult.OK)
{
fileName = saveFileExcel.FileName;
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;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
app.Quit();
workbook = null;
app = null;
}
}
else
{
MessageBox.Show("No Records");
}
No comments:
Post a Comment