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

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

            }

        }

    }

}



posted @ 2007-12-12 11:09  K!ngZ  阅读(573)  评论(0编辑  收藏  举报