export data to word in c#
  • hi today i am sharing how can we export data to ms word from c# windows application.
    Suppose we have form who generate bill for customer and we want to print this bill into word so we can use this method.
    You can also download attach source code project.

    This is our windows form with data.

    image

    and this will generate one ms word file when we export it.

    word file is this.

    image

    now come to coding side for generate a doc file.

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace WindowsApplication1
    {
    public partial class Form1 : Form
    {

    public Form1()
    {
    InitializeComponent();

    }
    SaveFileDialog sfd = new SaveFileDialog();

    private void Form1_Load(object sender, EventArgs e)
    {
    dgDetail.DataSource = GetSampleTable();
    }
    public DataTable GetSampleTable()
    {
    // Create sample table.
    DataTable dt = new DataTable();

    dt.Columns.Add("Product");
    dt.Columns.Add("Quantity");
    dt.Columns.Add("Rate");

    dt.Rows.Add();

    dt.Rows[0]["Product"] = "Product A";
    dt.Rows[0]["Quantity"] = 10;
    dt.Rows[0]["Rate"] = 25;

    dt.Rows.Add();

    dt.Rows[1]["Product"] = "Product B";
    dt.Rows[1]["Quantity"] = 5;
    dt.Rows[1]["Rate"] = 50;

    dt.Rows.Add();

    dt.Rows[2]["Product"] = "Product C";
    dt.Rows[2]["Quantity"] = 15;
    dt.Rows[2]["Rate"] = 20;

    return dt;
    }

    private void btnWord_Click(object sender, EventArgs e)
    {
    sfd.Filter = "Word Documents (*.doc)|*.doc";

    if (sfd.ShowDialog() == DialogResult.OK)
    {
    ExportToWord();
    }

    }
    private void ExportToWord()
    {

    string strForPrint = "";

    //writing bill fields
    strForPrint += "Bill No : " + txtBillNo.Text +"\t";
    strForPrint += "Date : " + dtpDate.Text + "\r\n\r\n";
    strForPrint += "Customer Name : " + txtCustomer.Text + "\r\n\r\n";
    strForPrint += "Remarks : " + txtRemarks.Text + "\r\n\r\n\r\n";
    strForPrint += "-----Bill Detail-----" + "\r\n\r\n\r\n";


    // writing datagridview column titles:

    string strHeaderTitle = "";

    for (int j = 0; j < dgDetail.Columns.Count; j++)
    {
    strHeaderTitle = strHeaderTitle.ToString() + Convert.ToString(dgDetail.Columns[j].HeaderText) + "\t\t";
    }

    strForPrint += strHeaderTitle + "\r\n";

    // writing datagridview data.

    for (int i = 0; i < dgDetail.RowCount - 1; i++)
    {
    string strLineData = "";

    for (int j = 0; j < dgDetail.Rows[i].Cells.Count; j++)
    {
    strLineData = strLineData.ToString() + Convert.ToString(dgDetail.Rows[i].Cells[j].Value);
    if (j == 1)
    {
    strLineData = strLineData + "\t\t\t";
    }
    else
    {
    strLineData = strLineData + "\t\t";
    }
    }
    strForPrint += strLineData + "\r\n";
    }

    Encoding utf16 = Encoding.GetEncoding(1254);


    byte[] output = utf16.GetBytes(strForPrint);

    FileStream fs = new FileStream(sfd.FileName, FileMode.Create);

    BinaryWriter bw = new BinaryWriter(fs);

    bw.Write(output, 0, output.Length); //write data into file

    bw.Flush();

    bw.Close();

    fs.Close();

    }
    }
    }
    Bill.jpg
    998 x 589 - 78K
    BillGrid.jpg
    447 x 496 - 45K
    WindowsApplication1.zip
    64K
  • 1 Comment sorted by
  • I have a form that contains two elements: a CheckedListBox and a CheckBox. The CheckBox, which is called SelectAllCheckBox, is used to check/uncheck all items in the CheckedListBox. I achieve this via a CheckedChanged event handler associated with the SelectAllCheckBox, so that when it is checked, all items in the CheckedListBox are checked, and vice versa. This works fine.

    I also have code that unchecks the SelectAllCheckBox when the user unchecks one of the CheckBoxes in the CheckedListBox. For example, if the user checks the SelectAllCheckBox, and then unchecks one of the items, the Select All CheckBox should be unchecked. This is achieved via the CheckedListBox.ItemChecked event handler. This also works fine.

    My problem is that when the SelectAllCheckBox is programatically unchecked (as in the above scenario), its event handler causes all items in the CheckedListBox to become unchecked.

    I'm sure others have run into my issue; is there an elegant workaround?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

Optimum Creative