C#實現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法
更新時間:2015年08月12日 12:17:09 作者:dyx2010
這篇文章主要介紹了C#實現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法,涉及C#操作word及Excel格式文件的方法,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#實現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法。分享給大家供大家參考。具體如下:
void OutToWord()
{
if (dataGridView1.Rows.Count >= 1)
{
string tempstr ="";
Stream myStream;
SaveFileDialog dlg =new SaveFileDialog();
dlg.Filter = "(Word文件)*.doc|*.doc";
//dlg.Filter = "(Word文件)*.xls|*.xls";
dlg.FilterIndex = 2;
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() ==DialogResult.OK)
{
if ((myStream = dlg.OpenFile()) !=null)
{
StreamWriter sw =new StreamWriter(myStream,Encoding.GetEncoding("gb2312"));
string str ="";
try
{
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i > 0) str +="\t\t";
str += dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
for (int j = 0; j < dataGridView1.Rows.Count - 1; j++)
{
tempstr = "";
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (k > 0) tempstr +="\t\t";
tempstr += dataGridView1.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempstr);
}
sw.Close();
myStream.Close();
MessageBox.Show("已將信息導(dǎo)出到word","提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
}
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
C#/VB.NET實現(xiàn)在PDF文檔中插入,替換或刪除圖片
這篇文章主要為大家詳細(xì)介紹了如何使用 Spire.PDF for .NET 通過程序在 PDF 文檔中插入、替換或刪除圖片,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
C#客戶端程序調(diào)用外部程序的3種實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于C#客戶端程序調(diào)用外部程序的3種實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
C#實現(xiàn)AddRange為數(shù)組添加多個元素的方法
這篇文章主要介紹了C#實現(xiàn)AddRange為數(shù)組添加多個元素的方法,實例分析了AddRange方法的使用技巧,需要的朋友可以參考下2015-06-06
C#中將DataTable轉(zhuǎn)化成List<T>的方法解析
大家應(yīng)該都知道在.net項目中使用到DataTable和List<T>集合的地方較多,有的時候需要將DataTable轉(zhuǎn)化成List<T>,那么改如何轉(zhuǎn)化呢?下面通過這篇文章來一起學(xué)習(xí)下吧,本文中給出了詳細(xì)的示例代碼,相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價值。2016-12-12

