因為機子是公司的,連不上數據庫,不知道怎麼整地,反正自己的權限很小。只好在空間裡添加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++)
}
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();
}
}
}