110,539
社区成员
发帖
与我相关
我的任务
分享
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
if (of.ShowDialog() == DialogResult.OK)
{
byte[] bt = wordConvertByte(of.FileName);
string ss = ByTeConvertWord(bt, "QQ1");
label1.Text = ss;
}
}
/// <summary>
/// 创建word
/// </summary>
/// <param name="data"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public string ByTeConvertWord(byte[] data, string fileName)
{
string savaPath = @"\SystemWord\" + DateTime.Now.Ticks.ToString() + @"\";
if (!System.IO.Directory.Exists(GetPath() + savaPath))
{
Directory.CreateDirectory(GetPath() + savaPath);
}
string filepath = GetPath() + savaPath + fileName + ".doc";
FileStream fs;
if (System.IO.File.Exists(filepath))
{
fs = new FileStream(filepath, FileMode.Truncate);
}
else
{
fs = new FileStream(filepath, FileMode.CreateNew);
}
BinaryWriter br = new BinaryWriter(fs);
br.Write(data, 0, data.Length);
br.Close();
fs.Close();
return filepath;
}
/// <summary>
/// 读取word转化成进制
/// </summary>
/// <param name="wordpath"></param>
/// <returns></returns>
private byte[] wordConvertByte(string wordpath)
{
byte[] byContent = null;
System.IO.FileStream fs = null;
System.IO.BinaryReader br = null;
try
{
fs = new FileStream(wordpath, System.IO.FileMode.Open);
}
catch
{
}
br = new BinaryReader((Stream)fs);
byContent = br.ReadBytes((Int32)fs.Length);
return byContent;
}
private string GetPath()
{
return Application.StartupPath;
}