C#中如何将含有文字、图片或表格的二进制数据写入到word文档的书签位置 目前已经实现了通过在word文档中设置书签,在程序中将文字写入到该书签位置,用的是range.text但如果现在的内容里包含了文字和图片或者是表格,那该怎么将这些内容都写到这个书签位置呢??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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; } 谢谢zyloveyrf的回复,但我不是要另存成一个word文件,而是现在已经有一个word文件了,我要在这个word文件中的书签位置插入哪些二进制数据。 实现winform定时锁定功能 DLL调用值C++ 对应的C#的问题 List.sort(泛型 Comparison)出现stack overflow 将参数值从 String 转换到 Int32 失败??? 怎么不刷新父窗口 flexgrid的cell的最大长度的问题。 时间比较的问题!! WinForm+定时更新数据库 如何让一个程序重新启动一下它自己?在线等ing.... 在一般的聊天工具中,怎样捕捉Ctrl+<CR>? 调试的时候发现个很奇怪的问题不知所以然. C#计算器的小问题~急!!!
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;
}