用户操作
[即时聊天] [发私信] [加为好友]
YanRocky
最近评论
文章分类
    收藏
      相册
      存档
      软件项目交易
      订阅我的博客
      XML聚合  FeedSky
      订阅到鲜果
      订阅到Google
      订阅到抓虾
      订阅到BlogLines
      订阅到Yahoo
      订阅到GouGou
      订阅到飞鸽
      订阅到Rojo
      订阅到newsgator
      订阅到netvibes

      原创 从数据库导出数据到word、excel、XML收藏

      新一篇: Create a new Meeting Request in Outlook 2007 using C# | 

       

      首先要把引用都加上

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
      using Microsoft.Office.Core;
      using Word = Microsoft.Office.Interop.Word;
      using System.IO;
      using System.Reflection;
      using System.Xml;
      using Excel = Microsoft.Office.Interop.Excel;
       
      namespace CreateTable
      {
          public partial class Form1 : Form
          {


              public Form1()
              {
                  InitializeComponent();
       
              }
              public void Creat_XML()
              {
               
                  String filename="D:/MyDoc/"+Nametxt.Text.ToString()+".xml";
                  myDataSet.Customer.WriteXml(@filename);
                  Nametxt.Text = ""; 
                  MessageBox.Show("The File is created sucessfully in D:/MyDoc");
               
                         }
              public void Creat_Excel()
              {
                  object omissing =System.Reflection.Missing.Value;
         
                  //Start excel and create a new file
                 // Directory.CreateDirectory("D:/MyDoc");
                  string fn=@"D:\MyDoc\" +Nametxt.Text+".xls";
                  object Exfilename = fn;

                  //Create new Excel Document
                  Excel1.Application myExcel = new Excel.ApplicationClass();
                  myExcel.Application.Workbooks.Add(omissing);
                  myExcel.Visible = true;

                  try
                  {
                       //Insert  new data
                      int totalCount = 0;
           
                          int row = myDataSet.Customer.Rows.Count;
                          int column = myDataSet.Customer.Columns.Count;

                          for (int i = 0; i < column; i++)
                          {
                              myExcel.Cells[totalCount + 1, 1 + i] = myDataSet.Customer.Columns[i].ColumnName;
                          }

                          for (int i = 0; i < row; i++)
                          {
                              for (int j = 0; j < column; j++)
                              {
                                  myExcel.Cells[totalCount + 2 + i, 1 + j] = "'" + myDataSet.Customer.Rows[i][j].ToString();
                              }
                          }
                     

                      }
                
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message);
                  }
                  myExcel.ActiveWorkbook._SaveAs(Exfilename, omissing, omissing, omissing, omissing,omissing,Excel.XlSaveAsAccessMode.xlExclusive, omissing, omissing, omissing,omissing);
                MessageBox.Show("The document is created sucessfully in D:/MyDoc");
                
              }
              public void Creat_Word()
              {
                  if (Nametxt.Text == "")
                  {
                      MessageBox.Show("Please Enter the File Name!");
                      return;
                  }

                  try
                  {

                      object oMissing = System.Reflection.Missing.Value;
                      object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

                      //Start Word and create a new document.
                      Directory.CreateDirectory("D:/MyDoc");
                      object FileName = "D://MyDoc//" + Nametxt.Text;
                      Word._Application oWord;
                      Word._Document oDoc;
                      oWord = new Word.Application();
                      oWord.Visible = false;
                      oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                          ref oMissing, ref oMissing);

                      //create a new table
                      Word.Table table;
                      Word.Range orang = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
                      table = oDoc.Tables.Add(orang, myDataSet.Customer.Rows.Count + 1, myDataSet.Customer.Columns.Count, ref oMissing, ref oMissing);
                      table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                      table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;

                      //Insert Data
                      for (int column = 0; column < myDataSet.Customer.Columns.Count; ++column)
                      {
                          table.Cell(1, column + 1).Range.Text = myDataSet.Customer.Columns[column].ColumnName.ToString().Trim();

                      }
                      for (int row = 0; row < myDataSet.Customer.Rows.Count; ++row)
                      {
                          for (int column = 0; column < myDataSet.Customer.Columns.Count; ++column)
                          {
                              table.Cell(row + 2, column + 1).Range.Text = myDataSet.Customer.Rows[row][column].ToString().Trim();

                          }
                      }

                      //Save the Document
                      oDoc.SaveAs(ref FileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                      oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                      oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
                      //Waitlabel.Text = "";
                      Nametxt.Text = "";
                      MessageBox.Show("The document is created sucessfully in D:/MyDoc");

                      //Close this form.
                      // this.Close();

                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message);

                  }
              }
              
              private void bt_CreatWord_Click(object sender, EventArgs e)
              {
                  if (Nametxt.Text == "")
                  {
                      MessageBox.Show("Please Enter the File Name!");
                      return;
                  }
                  MessageBox.Show("Please wait for a moment!");
                  Creat_Word();
                
               }

              private void bt_Exit_Click(object sender, EventArgs e)
              {
                  System.Windows.Forms.Application.Exit();
              }

              private void Form1_Load(object sender, EventArgs e)
              {
                  // TODO: This line of code loads data into the 'myDataSet.Customer' table. You can move, or remove it, as needed.
                  this.customerTableAdapter.Fill(this.myDataSet.Customer);

              }

              private void btn_XML_Click(object sender, EventArgs e)
              {
                  if (Nametxt.Text == "")
                  {
                      MessageBox.Show("Please Enter the File Name!");
                      return;
                  }
                  Creat_XML();
              }

              private void btn_excel_Click(object sender, EventArgs e)
              {
                  if (Nametxt.Text == "")
                  {
                      MessageBox.Show("Please Enter the File Name!");
                      return;
                  }
                  Creat_Excel();
              }
       


                
             
          }

       


      发表于 @ 2008年02月03日 17:09:00|评论(loading...)|编辑

      新一篇: Create a new Meeting Request in Outlook 2007 using C# | 

      评论:没有评论。

      发表评论  


      当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
      Csdn Blog version 3.1a
      Copyright © YanRocky