跪求。 office2007 .docx格式保存进数据库后读取出错!

lgmzyy 2009-08-19 01:21:45
先放出我的代码。
OpenFileDialog openFileDialog1 = new OpenFileDialog();
string filNm;
string mingc;
openFileDialog1.Multiselect = false;
openFileDialog1.Filter = "MS-Word Files (*.doc,*.dot,*.docx) | *.doc;*.dot;*.docx";
if (filNm == "")
{
MessageBox.Show("Please wait while the document is being displayed");
return;
}
FileStream fs = File.OpenRead(textBox2.Text);
byte[] content = new byte[fs.Length];
fs.Read(content, 0, content.Length);
fs.Close();
----------------------将*.docx文档转换为二进制数据---------------------


----连接数据库代码省略----
/// <summary>
/// 直接执行SQL语句(二进制流)
/// </summary>
public int RunProc(byte[] content, string mingcheng,string zm)
{
this.Open();
string sql = "INSERT INTO WDGL (WDIM,WDName,zm) VALUES (@Photos, @name,@zm)";
SqlCommand comm = new SqlCommand(sql, con);
comm.Parameters.Add("@Photos", SqlDbType.Image).Value = content;
comm.Parameters.Add("@name", SqlDbType.NVarChar).Value = mingcheng;
comm.Parameters.Add("@zm", SqlDbType.NVarChar).Value = zm;
comm.ExecuteNonQuery();
this.Close();
this.Dispose();
return 1;
}
--------------------------------利用如上代码 将二进制数据 保存到数据库中 -----------------------

private void ShowImage()
{
string str1 = System.Windows.Forms.Application.ExecutablePath //获取exe文件所在路径
AppDomain.CurrentDomain.BaseDirectory
string str5 = Application.StartupPath;
//取得路径
//string path = str1.ToLower().Replace("windowsapplication1.exe", "test.doc");
byte[] b = baseinfo.wendang(id);
if (b == null)
{
MessageBox.Show("文档无法读取!");
}
else
{
if (b.Length > 0)
{
int ArraySize = new int();
ArraySize = b.GetUpperBound(0);

FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(b, 0, ArraySize);
fs.Close();
string film = str5 + "\\test.doc";
try
{
objWinWordControl.CloseControl();
}
catch { }
finally
{
objWinWordControl.document = null;
WinWordControl.WinWordControl.wd = null;
WinWordControl.WinWordControl.wordWnd = 0;
}
try
{
objWinWordControl.LoadDocument(film);

}
catch (Exception ex) { String err = ex.Message; }
}
}
}
-------------------如上为恢复二进制的方法,再利用winWordControl显示恢复的.docx文件,失败------------------------------
但是用以上方法存取.doc文件没有问题。请各位高手大哥帮帮忙!!!!!
...全文
309 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
shineyes 2010-12-03
  • 打赏
  • 举报
回复
我对比了一下,写入数据库时,doc格式与docx格式文件的内容最后都多了一个CRLF。
读取出来,另存之后,doc格式的文件前面没有增加内容,docx格式的文件,前面又多了一个CRLF。
但是个人感觉应该不完全是这个问题。正在疑惑中。。。
shineyes 2010-12-03
  • 打赏
  • 举报
回复
帮顶,遇到同样的问题了。不知道楼主研究出来没有?
xiaowen_ly 2009-08-19
  • 打赏
  • 举报
回复
帮顶
YY_StartGO 2009-08-19
  • 打赏
  • 举报
回复
对啊 office2007的格式是*.docx 你不会读错吧
Lovely_baby 2009-08-19
  • 打赏
  • 举报
回复
不好意思 没接触过 帮顶吧
lgmzyy 2009-08-19
  • 打赏
  • 举报
回复
跪求 高手帮忙!!!!
lgmzyy 2009-08-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jinglecat 的回复:]
不清楚,不过一切值得怀疑的都试试

string film = str5 + "\\test.docx";

[/Quote]
上面的这段代码 只是完成了一个文件路径的功能,可以100%保证没有问题
lgmzyy 2009-08-19
  • 打赏
  • 举报
回复
不知道为什么 .docx文件转换成二进制数据后,再还原后 就该文档就提示已损坏。其他格式如:.doc,.bmp,.dwg都没问题
bychgh 2009-08-19
  • 打赏
  • 举报
回复
帮顶
Jinglecat 2009-08-19
  • 打赏
  • 举报
回复
不清楚,不过一切值得怀疑的都试试

string film = str5 + "\\test.docx";
tzs2304 2009-08-19
  • 打赏
  • 举报
回复
up
feifeiyiwen 2009-08-19
  • 打赏
  • 举报
回复
up
lgmzyy 2009-08-19
  • 打赏
  • 举报
回复
长度足够
IHandler 2009-08-19
  • 打赏
  • 举报
回复
数据库字段长度够吗
mbh0210 2009-08-19
  • 打赏
  • 举报
回复

private object Nothing = System.Reflection.Missing.Value;
Document doc = wordApp.Documents.Add(ref tempPath, ref Nothing, ref Nothing, ref Nothing);
string xml = doc.WordOpenXML;

将路径传进去就可以了
lgmzyy 2009-08-19
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 mbh0210 的回复:]
是不是编码的问题,看看你读出来的内容的是怎么样的
既然用2007了,你将word的xml保存到数据库就行了
[/Quote]
xml保存到数据库 请求赐教
mbh0210 2009-08-19
  • 打赏
  • 举报
回复
是不是编码的问题,看看你读出来的内容的是怎么样的
既然用2007了,你将word的xml保存到数据库就行了
lgmzyy 2009-08-19
  • 打赏
  • 举报
回复
继续顶 跪求高手帮忙

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧