|
|
#21
iceqier2007-03-30 18:04
第二题有解了,winfrom做的,答案如下: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Data.OleDb; using Office;
namespace Office文件转存 { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox txtbox; private System.Windows.Forms.Button btnload; private System.Windows.Forms.Button btnsave;
private OleDbConnection con; private OleDbDataAdapter dar; private DataSet ds;
/// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;
public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent();
// // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // }
/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }
#region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.txtbox = new System.Windows.Forms.TextBox(); this.btnload = new System.Windows.Forms.Button(); this.btnsave = new System.Windows.Forms.Button(); this.SuspendLayout(); // // txtbox // this.txtbox.Location = new System.Drawing.Point(24, 16); this.txtbox.Multiline = true; this.txtbox.Name = "txtbox"; this.txtbox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txtbox.Size = new System.Drawing.Size(472, 264); this.txtbox.TabIndex = 0; this.txtbox.Text = ""; // // btnload // this.btnload.Location = new System.Drawing.Point(104, 296); this.btnload.Name = "btnload"; this.btnload.Size = new System.Drawing.Size(96, 23); this.btnload.TabIndex = 1; this.btnload.Text = "Load"; this.btnload.Click += new System.EventHandler(this.btnload_Click); // // btnsave // this.btnsave.Location = new System.Drawing.Point(312, 296); this.btnsave.Name = "btnsave"; this.btnsave.Size = new System.Drawing.Size(96, 23); this.btnsave.TabIndex = 2; this.btnsave.Text = "Save"; this.btnsave.Click += new System.EventHandler(this.btnsave_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(520, 335); this.Controls.Add(this.btnsave); this.Controls.Add(this.btnload); this.Controls.Add(this.txtbox); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Office文件转存工具"; this.ResumeLayout(false);
} #endregion
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } //读取文档 private void btnload_Click(object sender, System.EventArgs e) { try { System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Word 文档(*.doc)|*.doc|PowerPoint 演示文稿(*.ppt)|*.ppt|Excel 文件(*.xls)|*.xls"; if(ofd.ShowDialog() == DialogResult.OK) { switch(ofd.FileName.Substring(ofd.FileName.LastIndexOf('.'),4)) { //读取Word文档文本 case ".doc": txtbox.Text=""; object docname = ofd.FileName; object readOnly = true; object missing = System.Reflection.Missing.Value; Word.ApplicationClass wapp = new Word.ApplicationClass(); Word.Document doc = wapp.Documents.Open(ref docname, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing); txtbox.Text = doc.Content.Text; break;
//读取PowerPoint文档文本 case ".ppt": txtbox.Text=""; PowerPoint.ApplicationClass pa = new PowerPoint.ApplicationClass(); PowerPoint.Presentation pp = pa.Presentations.Open(ofd.FileName,Office.MsoTriState.msoCTrue,Office.MsoTriState.msoFalse,Office.MsoTriState.msoFalse); foreach(PowerPoint.Slide slide in pp.Slides) { foreach(PowerPoint.Shape shape in slide.Shapes) { txtbox.Text = txtbox.Text + shape.TextFrame.TextRange.Text + System.Environment.NewLine; } } break; //读取Excel文档文本 case ".xls": txtbox.Text=""; string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ofd.FileName+";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; con = new OleDbConnection(str); con.Open(); string sql = "select * from [sheet1$]"; dar = new OleDbDataAdapter(sql,str); ds = new DataSet(); dar.Fill(ds); for(int i=0;i<ds.Tables[0].Rows.Count;i++) { for(int j=0;j<ds.Tables[0].Columns.Count;j++) { txtbox.Text += ds.Tables[0].Rows[i][j].ToString()+" "; } txtbox.Text += System.Environment.NewLine; } break; } } } catch { MessageBox.Show("文件读取错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { if(con != null) { ds.Dispose(); dar.Dispose(); con.Dispose(); } } }
private void btnsave_Click(object sender, System.EventArgs e) { try { System.Windows.Forms.SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "文本文件(*.txt)|*.txt"; if(sfd.ShowDialog() == DialogResult.OK) { StreamWriter sw = new StreamWriter(sfd.OpenFile()); sw.WriteLine(txtbox.Text); sw.Close(); MessageBox.Show("文件已保存", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch { MessageBox.Show("文件保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
|