Export Excel from DataGridView

Source: Internet
Author: User

There are two cases in which Excel is exported from DataGridView, not much, and the code is recorded directly.

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.Windows.Forms;

Using System.IO;

Using System.Data;

Namespace Cnblogs

{

public static Class Loadexcel

{

public static void Datatoexcel (DataGridView m_dataview,string s)

{

SaveFileDialog KK = new SaveFileDialog ();

Kk. Title = "Save Execl file";

Kk. Filter = "Execl file (*.xls) |*.xls | All Files (*. *) |*.*";

Kk. FilterIndex = 1;

if (KK. ShowDialog () = = DialogResult.OK)

{

String FileName = Kk. filename;//+ ". xls";

if (file.exists (FileName))

File.delete (FileName);

FileStream objFileStream;

StreamWriter objStreamWriter;

String strLine = "";

objFileStream = new FileStream (FileName, FileMode.OpenOrCreate, FileAccess.Write);

objStreamWriter = new StreamWriter (objFileStream, System.Text.Encoding.Unicode);

Objstreamwriter.writeline (s); Statistical results Information

for (int i = 0; i < M_DataView.Columns.Count; i++)

{

if (M_dataview.columns[i]. Visible = = True)

{

StrLine = StrLine + m_dataview.columns[i]. Headertext.tostring () + Convert.tochar (9);

}

}

Objstreamwriter.writeline (StrLine);

StrLine = "";

for (int i = 0; i < M_DataView.Rows.Count; i++)

{

if (M_dataview.columns[0]. Visible = = True)

{

if (M_dataview.rows[i]. Cells[0]. Value = = null)

StrLine = StrLine + "" + Convert.tochar (9);

Else

StrLine = StrLine + m_dataview.rows[i]. Cells[0]. Value.tostring () + Convert.tochar (9);

}

for (int j = 1; j < M_DataView.Columns.Count; J + +)

{

if (M_dataview.columns[j]. Visible = = True)

{

if (M_dataview.rows[i]. CELLS[J]. Value = = null)

StrLine = StrLine + "" + Convert.tochar (9);

Else

{

String rowstr = "";

Rowstr = M_dataview.rows[i]. CELLS[J]. Value.tostring ();

if (rowstr. IndexOf ("\ r \ n") > 0)

Rowstr = Rowstr. Replace ("\ r \ n", "");

if (rowstr. IndexOf ("T") > 0)

Rowstr = Rowstr. Replace ("\ T", "");

StrLine = strLine + rowstr + convert.tochar (9);

}

}

}

Objstreamwriter.writeline (StrLine);

StrLine = "";

}

Objstreamwriter.close ();

Objfilestream.close ();

MessageBox.Show ("Export succeeded! ");

}

}

Use DataGridView bound DataTable as parameter, export Excel

public static void Datatoexcel (DataTable m_datatable)

{

SaveFileDialog KK = new SaveFileDialog ();

Kk. Title = "Save Execl file";

Kk. Filter = "Execl file (*.xls) |*.xls | All Files (*. *) |*.*";

Kk. FilterIndex = 1;

if (KK. ShowDialog () = = DialogResult.OK)

{

String FileName = Kk. FileName + ". xls";

if (file.exists (FileName))

File.delete (FileName);

FileStream objFileStream;

StreamWriter objStreamWriter;

String strLine = "";

objFileStream = new FileStream (FileName, FileMode.OpenOrCreate, FileAccess.Write);

objStreamWriter = new StreamWriter (objFileStream, System.Text.Encoding.Unicode);

for (int i = 0; i < M_DataTable.Columns.Count; i++)

{

StrLine = StrLine + m_datatable.columns[i]. Caption.tostring () + Convert.tochar (9);

}

Objstreamwriter.writeline (StrLine);

StrLine = "";

for (int i = 0; i < M_DataTable.Rows.Count; i++)

{

for (int j = 0; J < M_DataTable.Columns.Count; J + +)

{

if (M_datatable.rows[i]. ITEMARRAY[J] = = null)

StrLine = StrLine + "" + Convert.tochar (9);

Else

{

String rowstr = "";

Rowstr = M_datatable.rows[i]. ITEMARRAY[J]. ToString ();

if (rowstr. IndexOf ("\ r \ n") > 0)

Rowstr = Rowstr. Replace ("\ r \ n", "");

if (rowstr. IndexOf ("T") > 0)

Rowstr = Rowstr. Replace ("\ T", "");

StrLine = strLine + rowstr + convert.tochar (9);

}

}

Objstreamwriter.writeline (StrLine);

StrLine = "";

}

Objstreamwriter.close ();

Objfilestream.close ();

}

}

}

}

Export Excel from DataGridView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.