如何在c#中编辑word文档,并存入数据库?

clevery 2003-09-15 11:52:41
如何在c#中编辑word文档,并存入数据库?
...全文
359 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
BRainHart 2003-09-17
  • 打赏
  • 举报
回复
中间注释的代码是用来读写图片的.有兴趣的话可以试一试.DrowToScale的代码你可以写的非常简单,把你的图片传给控件就可以了.
BRainHart 2003-09-17
  • 打赏
  • 举报
回复
至少到目前为止,我还没有找到一个可以直接从二进制流读出来直接进行编缉而不进行保存的.在这里也问过,不过没有答案.所以,只好把二进制流读出来保存到硬盘上,再进行打开编辑,当WORD关闭时,把它删掉.
方法和保存到数据库大概是一样的,只不过是由saveFileDialog来完成的,而且FileMode要改变一下.
try
{
fileCommand=new SqlCommand("SELECT FileBody FROM tblFile WHERE FileName='"+comboBox1.Text+"'",fileConnection);
byte[] b=(byte[])fileCommand.ExecuteScalar();
string file=comboBox1.Text;
/* if(b.Length > 0)
{
// Open a stream for the image and write the bytes into it
System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
stream.Write(b, 0, b.Length);

// Draw photo to scale of picturebox
DrawToScale(new Bitmap(stream));

// Close the stream and delete the temp file
stream.Close();
}*/
if(b.Length>0)
{
System.IO.MemoryStream stream=new MemoryStream(b,true);
//stream.Write(b,0,b.Length);
saveFileDialog1.FileName=file;
if(DialogResult.OK==saveFileDialog1.ShowDialog())
{

System.IO.FileStream aa=new FileStream(saveFileDialog1.FileName,System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.Write);
aa.Write(b,0,b.Length);

stream.Close();
}
}
comboBox1.Text是你在数据库中存储的WORD文件的名字.
还要记得把数据库名字改掉:)
yyfa6 2003-09-16
  • 打赏
  • 举报
回复
gz
clevery 2003-09-16
  • 打赏
  • 举报
回复
还有一个问题,如何把存到数据库的word文档打开?
BRainHart 2003-09-15
  • 打赏
  • 举报
回复
首先是建立Word的实例.
需要
Microsoft.Office.Interop.Word.dll
Office.dll

Application app = new Application();
object template=Missing.Value;
object newTemplate=Missing.Value;
object documentType=Missing.Value;
object visible=true;
_Document doc = app.Documents.Add( ref template,ref newTemplate,ref documentType,ref visible);

http://www.yesky.com/20030208/1651044.shtml
然后是存入数据库
private void LoadIn_Click(object sender, System.EventArgs e)
{
if(DialogResult.OK==openFileDialog1.ShowDialog())
{
foreach(string filename in openFileDialog1.FileNames)
{
System.IO.FileStream stream=new FileStream(filename,System.IO.FileMode.Open,System.IO.FileAccess.Read);
byte[] buffer=new byte[stream.Length];
stream.Read(buffer,0,(int)buffer.Length);
stream.Close();
InsertFile(ref buffer,filename);
}

}
}
void InsertFile(ref byte[] buffer,string filename)
{
try
{

comm=new SqlCommand ("insert into tblFile values(@filename,@filebody)",conn);
comm.Parameters.Add("@filename",SqlDbType.NVarChar).Value=filename;
comm.Parameters.Add("@filebody",SqlDbType.Image).Value=buffer;
comm.ExecuteNonQuery();
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}

}
至于COM我想也不会太难了吧
clevery 2003-09-15
  • 打赏
  • 举报
回复
我的本意不是要在c#中编辑word文档,是不是能用ole实现?soulroom说用com方式,请具体点,我是新手。
先谢了
HenanBoy 2003-09-15
  • 打赏
  • 举报
回复
在C#中编辑Word我感觉这个东西比较难了。不过要是存如数据库比较容易了早网上很多了。你找找看了
soulroom 2003-09-15
  • 打赏
  • 举报
回复
你可以用Com的方式调用word文档,然后将文件的二进制流存入数据库

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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