程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 從數據庫導出數據到word、excel、XML

從數據庫導出數據到word、excel、XML

編輯:.NET實例教程

因為機子是公司的,連不上數據庫,不知道怎麼整地,反正自己的權限很小。只好在空間裡添加Dataset 來操作數據。

首先要把引用都加上

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/sad.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 + 2, 1 + i] = myDataSet.Customer.Columns[i].ColumnName;
                    }

                    for (int i = 0; i < row; i++)
             

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

             &nbsp;  }
          
            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 == "")
       &nbsp;    {
                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
              &nbsp; 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!");
         &nbsp;      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();
        }
 


          
       
    }

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved