| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1252 人关注过本帖
标题:[求助]两道派凯测试题目
只看楼主 加入收藏
iceqier
Rank: 1
等 级:新手上路
威 望:1
帖 子:129
专家分:0
注 册:2006-1-22
收藏
得分:0 
第二题有解了,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);
}
}
}
}

天天摸键盘,整天看代码。。。 E-mail : iceqier520@
2007-03-30 18:04
快速回复:[求助]两道派凯测试题目
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027372 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved