跪求。 office2007 .docx格式保存进数据库后读取出错!
先放出我的代码。
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文件没有问题。请各位高手大哥帮帮忙!!!!!
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文件没有问题。请各位高手大哥帮帮忙!!!!!
...全文
相关推荐
Office2007 文件格式兼容包
安装完成后,用户无需重启电脑,即可在旧版Office中打开、编辑和保存.docx、.pptx和.xlsx文件。
Delphi DOCXReadWrite控件:无Office依赖的Word文档自动化生成与解析
在软件开发和办公自动化领域,文档处理是常见的核心需求,尤其是对Microsoft Word文档的读写操作。传统上,开发者可能依赖OLE自动化或商业组件,但前者存在依赖性强、性能差的问题,后者则成本较高。随着Open XML格式(.docx)的普及,其基于ZIP和XML的结构为第三方原生处理提供了可能。通过解析和操作内部的XML文件,可以实现不依赖Office软件的文档生成、解析与编辑,这为开发轻量、高效的文档处理模块提供了技术基础。其技术价值在于提升了自动化流程的稳定性和部署便利性,特别适用于服务器端批量处
office2003打开2007以上文件插件
在Office 2007中,微软引入了新的文件格式,例如.docx取代了传统的.doc,这导致了兼容性问题,因为早期版本的Office无法识别这些新格式。
office 2003打开Microsoft Office 2007 文件格式兼容包
**保存为旧版格式**:尽管可以读取新格式,但用户在编辑后仍然可以选择将文件保存为原有的DOC、XLS和PPT格式,以便于与仍在使用旧版Office的同事或合作伙伴共享。4.
office2003文件格式兼容包Office2007
标题中的“Office2003文件格式兼容包Office2007”指的是一个特殊软件工具,它的主要功能是让使用Microsoft Office 2003的用户能够读取、编辑和创建由更新版本的Office


