DataGridView 写入到EXCEL

 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();
                }

                //标题
                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;

                //数据
                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
            {
                //关闭
                app.UserControl = false;
                app.Quit();
            }
            return true;

        }

 

        internal static bool 写入xml(DataGridView dataGridView, string 文件名)
        {
            XmlTextWriter writer = null;
            bool 处理结果;
            try
            {
                writer = new XmlTextWriter(文件名, null);
                writer.Formatting = Formatting.Indented;    //为使文件易读,使用缩进
                writer.WriteComment("采集的数据");    //写入注释
                writer.WriteStartElement("信息列表");
                int 行 = dataGridView.Rows.Count;
                int 列 = dataGridView.Columns.Count;
                string[] 标题 = new string[列];
                for (int j = 0; j < 列; j++)
                {
                    标题[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(标题[j], 信息);
                    }
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                writer.Flush();
                writer.Close();

                处理结果 = true;

            }
            catch (Exception aa)
            {
                MessageBox.Show(aa.Message);
                处理结果 = false;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
               
            }
            return 处理结果;
        }
     

        //XML序列化写入
        //string 文件名 = AppDomain.CurrentDomain.BaseDirectory + "//配置.xml";
        //T 为可序列化的类
        public static bool 写入配置<T>(string 文件名, T 任务A)
        {
            try
            {
                XmlSerializer XML序列 = new XmlSerializer(typeof(T));
                Stream 数据流 = new FileStream(文件名, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                XML序列.Serialize(数据流, 任务A);
                数据流.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 结果 = (T)xs.Deserialize(stream);
                stream.Close();
                return 结果;
            }
            catch (Exception ee)
            {
                if (stream != null)
                    stream.Close();
                MessageBox.Show(ee.Message, "读取失败");
                return default(T);
            }          
        }


        public static bool 写入TXT(DataGridView 数据表, string 文件名)
        {

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

            StringBuilder s = new StringBuilder();
            int 行数 = 数据表.Rows.Count;
            int 列数 = 数据表.Columns.Count;
            for (int i = 0; i < 行数; i++)
            {
                for (int j = 0; j < 列数; j++)
                {
                    if (数据表[j, i].Value != null)
                    {
                        s.AppendLine(数据表[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, "写文本文件发生错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            finally
            {
                if (MyWriter != null)
                {
                    MyWriter.Close();
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值