日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DataGridView 写入到EXCEL

發(fā)布時間:2023/12/9 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataGridView 写入到EXCEL 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Windows.Forms;
using??Microsoft.Office.Interop.Excel;
using?System.Xml;
using?System.Xml.Serialization;
using?System.IO;

namespace?txt批處理
{
????
class?保存
????
{
????????
internal?static?bool?寫入EXCEL(DataGridView?gridView,?string?fileName,?bool?isShowExcle)
????????
{


????????????Microsoft.Office.Interop.Excel.Application?app?
=?new?Microsoft.Office.Interop.Excel.Application();
????????????
try
????????????
{
????????????????
if?(app?==?null)
????????????????
{
????????????????????
return?false;
????????????????}

????????????????app.Visible?
=?isShowExcle;
????????????????Workbooks?workbooks?
=?app.Workbooks;
????????????????_Workbook?workbook?
=?workbooks.Add(XlWBATemplate.xlWBATWorksheet);
????????????????Sheets?sheets?
=?workbook.Worksheets;
????????????????_Worksheet?worksheet?
=?(_Worksheet)sheets.get_Item(1);
????????????????
if?(worksheet?==?null)
????????????????
{
????????????????????
return?false;
????????????????}

????????????????
string?sLen?=?"";
????????????????
//取得最后一列列名
????????????????char?H?=?(char)(64?+?gridView.ColumnCount?/?26);
????????????????
char?L?=?(char)(64?+?gridView.ColumnCount?%?26);
????????????????
if?(gridView.ColumnCount?<?26)
????????????????
{
????????????????????sLen?
=?L.ToString();
????????????????}

????????????????
else
????????????????
{
????????????????????sLen?
=?H.ToString()?+?L.ToString();
????????????????}


????????????????
//標(biāo)題
????????????????string?sTmp?=?sLen?+?"1";
????????????????Range?ranCaption?
=?worksheet.get_Range(sTmp,?"A1");
????????????????
string[]?asCaption?=?new?string[gridView.ColumnCount];
????????????????
for?(int?i?=?0;?i?<?gridView.ColumnCount;?i++)
????????????????
{
????????????????????asCaption[i]?
=?gridView.Columns[i].HeaderText;
????????????????}

????????????????ranCaption.Value2?
=?asCaption;

????????????????
//數(shù)據(jù)
????????????????for?(int?r?=?0;?r?<?gridView.RowCount;?r++)
????????????????
{
????????????????????
for?(int?l?=?0;?l?<?gridView.Columns.Count;?l++)
????????????????????
{
????????????????????????
if?(gridView[l,?r].Value?!=?null)
????????????????????????
{
????????????????????????????worksheet.Cells[r?
+?2,?l?+?1]?=?gridView[l,?r].Value.ToString().Trim();
????????????????????????}

????????????????????}

????????????????}



????????????????workbook.SaveCopyAs(fileName);
????????????????workbook.Saved?
=?true;
????????????????workbook.Close(
false,?true,?null);
????????????}

????????????
catch
????????????
{
????????????????
return?false;
????????????}

????????????
finally
????????????
{
????????????????
//關(guān)閉
????????????????app.UserControl?=?false;
????????????????app.Quit();
????????????}

????????????
return?true;

????????}


?

????????
internal?static?bool?寫入xml(DataGridView?dataGridView,?string?文件名)
????????
{
????????????XmlTextWriter?writer?
=?null;
????????????
bool?處理結(jié)果;
????????????
try
????????????
{
????????????????writer?
=?new?XmlTextWriter(文件名,?null);
????????????????writer.Formatting?
=?Formatting.Indented;????//為使文件易讀,使用縮進
????????????????writer.WriteComment("采集的數(shù)據(jù)");????//寫入注釋
????????????????writer.WriteStartElement("信息列表");
????????????????
int?行?=?dataGridView.Rows.Count;
????????????????
int?列?=?dataGridView.Columns.Count;
????????????????
string[]?標(biāo)題?=?new?string[列];
????????????????
for?(int?j?=?0;?j?<?列;?j++)
????????????????
{
????????????????????標(biāo)題[j]?
=?dataGridView.Columns[j].HeaderText;
????????????????}

????????????????
for?(int?i?=?0;?i?<?行;?i++)
????????????????
{
????????????????????writer.WriteStartElement(
"信息");
????????????????????
for?(int?j?=?0;?j?<?列;?j++)
????????????????????
{??????????????????????
????????????????????????
string?信息?=?dataGridView[j,?i].Value?!=?null???dataGridView[j,?i].Value.ToString()?:?"";
????????????????????????writer.WriteElementString(標(biāo)題[j],?信息);
????????????????????}

????????????????????writer.WriteEndElement();
????????????????}

????????????????writer.WriteEndElement();
????????????????writer.Flush();
????????????????writer.Close();

????????????????處理結(jié)果?
=?true;

????????????}

????????????
catch?(Exception?aa)
????????????
{
????????????????MessageBox.Show(aa.Message);
????????????????處理結(jié)果?
=?false;
????????????}

????????????
finally
????????????
{
????????????????
if?(writer?!=?null)
????????????????
{
????????????????????writer.Close();
????????????????}

????????????????
????????????}

????????????
return?處理結(jié)果;
????????}

??????

????????
//XML序列化寫入
????????
//string?文件名?=?AppDomain.CurrentDomain.BaseDirectory?+?"\\配置.xml";
????????
//T?為可序列化的類
????????public?static?bool?寫入配置<T>(string?文件名,?T?任務(wù)A)
????????
{
????????????
try
????????????
{
????????????????XmlSerializer?XML序列?
=?new?XmlSerializer(typeof(T));
????????????????Stream?數(shù)據(jù)流?
=?new?FileStream(文件名,?FileMode.Create,?FileAccess.Write,?FileShare.ReadWrite);
????????????????XML序列.Serialize(數(shù)據(jù)流,?任務(wù)A);
????????????????數(shù)據(jù)流.Close();

????????????????
return?true;
????????????}

????????????
catch?(Exception?ee)
????????????
{
????????????????
return?false;
????????????}

????????}



????????
//XML序列化讀出????
????????
//T?為可反序列化的類
????????public?static?T?讀出配置<T>(string?文件名)
????????
{????????????
????????????XmlSerializer?xs?
=?new?XmlSerializer(typeof(T));
????????????Stream?stream?
=?null;
????????????
try
????????????
{
????????????????stream?
=?new?FileStream(文件名,?FileMode.Open,?FileAccess.Read,?FileShare.ReadWrite);
????????????????T?結(jié)果?
=?(T)xs.Deserialize(stream);
????????????????stream.Close();
????????????????
return?結(jié)果;
????????????}

????????????
catch?(Exception?ee)
????????????
{
????????????????
if?(stream?!=?null)
????????????????????stream.Close();
????????????????MessageBox.Show(ee.Message,?
"讀取失敗");
????????????????
return?default(T);
????????????}
???????????
????????}



????????
public?static?bool?寫入TXT(DataGridView?數(shù)據(jù)表,?string?文件名)
????????
{

????????????
if?(文件名.Trim()?==?"")
????????????????
return?false;

????????????StringBuilder?s?
=?new?StringBuilder();
????????????
int?行數(shù)?=?數(shù)據(jù)表.Rows.Count;
????????????
int?列數(shù)?=?數(shù)據(jù)表.Columns.Count;
????????????
for?(int?i?=?0;?i?<?行數(shù);?i++)
????????????
{
????????????????
for?(int?j?=?0;?j?<?列數(shù);?j++)
????????????????
{
????????????????????
if?(數(shù)據(jù)表[j,?i].Value?!=?null)
????????????????????
{
????????????????????????s.AppendLine(數(shù)據(jù)表[j,?i].Value.ToString().Trim()?);
????????????????????}

????????????????}

????????????????s.AppendLine(
"");
????????????}

????????????
????????????StreamWriter?MyWriter?
=?null;
????????????
try
????????????
{
????????????????MyWriter?
=?new?StreamWriter(文件名,?false,?System.Text.Encoding.Default);
????????????????MyWriter.Write(s);
????????????????
return?true;
????????????}

????????????
catch?(Exception?Err)
????????????
{
????????????????MessageBox.Show(Err.Message,?
"寫文本文件發(fā)生錯誤",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
????????????????
return?false;
????????????}

????????????
finally
????????????
{
????????????????
if?(MyWriter?!=?null)
????????????????
{
????????????????????MyWriter.Close();
????????????????}

????????????}

????????}

????}

}



轉(zhuǎn)載于:https://www.cnblogs.com/zhy4606/archive/2007/12/12/991891.html

總結(jié)

以上是生活随笔為你收集整理的DataGridView 写入到EXCEL的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。