word导入到datagridview 最近做单位的一个开发,需要对报表数据进行处理,报表是word上面手工画的表格,word能不能导入到datagridview,查阅了下网上没有提供案例,而excel导入到datagridview已经能够成功实行.有无成功实行的师兄们能不能赐教下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先把word中的表格数据都读出来,然后再想办法弄到DataGridView里面吧http://www.cnblogs.com/duguguiyu/archive/2006/08/11/474400.html 谢谢师兄的帮忙,但是研究了下代码工作量很大,因为要导入的word里面有图片,还有一些无序的手绘表格,自己也查阅了下网上的资料,好像用datagridview来解决不太现实,因而还是想采用excel导入的方式. 具体的流程是需要把下面部门上传的报表,现在的格式是word,里面包括有不确定行数的表格,而且表格里面还嵌有照片,在c#里面打开后,检查数据的正确性,并将数据保存到数据库里面.现在我想采取的方针还是导入excel的方案 public class BiultReportForm { /// /// word 应用对象 /// private Microsoft.Office.Interop.Word.Application _wordApplication; /// /// word 文件对象 /// private Microsoft.Office.Interop.Word.Document _wordDocument; /// /// 创建文档 /// public void CreateAWord() { //实例化word应用对象 this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); Object myNothing = System.Reflection.Missing.Value; this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing); } /// /// 添加页眉 /// /// public void SetPageHeader(string pPageHeader) { //添加页眉 this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView; this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader); //设置中间对齐 this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; //跳出页眉设置 this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; } /// /// 插入文字 /// /// 文本信息 /// 字体打小 /// 字体颜色 /// 字体粗体 /// 方向 public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment) { //设置字体样式以及方向 this._wordApplication.Application.Selection.Font.Size = pFontSize; this._wordApplication.Application.Selection.Font.Bold = pFontBold; this._wordApplication.Application.Selection.Font.Color= pFontColor; this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment; this._wordApplication.Application.Selection.TypeText(pText); } /// /// 换行 /// public void NewLine() { //换行 this._wordApplication.Application.Selection.TypeParagraph(); } /// /// 插入一个图片 /// /// public void InsertPicture(string pPictureFileName) { object myNothing = System.Reflection.Missing.Value; //图片居中显示 this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing); } /// /// 保存文件 /// /// 保存的文件名 public void SaveWord(string pFileName) { object myNothing = System.Reflection.Missing.Value; object myFileName = pFileName; object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument; object myLockd = false; object myPassword = ""; object myAddto = true; try { this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword, ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing, ref myNothing, ref myNothing, ref myNothing); } catch { throw new Exception("导出word文档失败!"); } } } LZ把代码考出开 弄下格式 我copy的我的项目的在文本框里面 没有格式 呵呵 拿人钱财 替人消灾public class BiultReportForm { /// /// word 应用对象 /// private Microsoft.Office.Interop.Word.Application _wordApplication; /// /// word 文件对象 /// private Microsoft.Office.Interop.Word.Document _wordDocument; /// /// 创建文档 /// public void CreateAWord() { //实例化word应用对象 this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); Object myNothing = System.Reflection.Missing.Value; this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing); } /// /// 添加页眉 /// /// public void SetPageHeader(string pPageHeader) { //添加页眉 this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView; this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader); //设置中间对齐 this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; //跳出页眉设置 this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; } /// /// 插入文字 /// /// 文本信息 /// 字体打小 /// 字体颜色 /// 字体粗体 /// 方向 public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment) { //设置字体样式以及方向 this._wordApplication.Application.Selection.Font.Size = pFontSize; this._wordApplication.Application.Selection.Font.Bold = pFontBold; this._wordApplication.Application.Selection.Font.Color= pFontColor; this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment; this._wordApplication.Application.Selection.TypeText(pText); } /// /// 换行 /// public void NewLine() { //换行 this._wordApplication.Application.Selection.TypeParagraph(); } /// /// 插入一个图片 /// /// public void InsertPicture(string pPictureFileName) { object myNothing = System.Reflection.Missing.Value; //图片居中显示 this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing); } /// /// 保存文件 /// /// 保存的文件名 public void SaveWord(string pFileName) { object myNothing = System.Reflection.Missing.Value; object myFileName = pFileName; object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument; object myLockd = false; object myPassword = ""; object myAddto = true; try { this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword, ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing, ref myNothing, ref myNothing, ref myNothing); } catch { throw new Exception("导出word文档失败!"); } } } 问一个 架构 的问题 窗口创建句柄时错误 c#问题 谁有多线程代码!!我想实现10个线程同时执行一个方法! 程序集“D:\项目\FolderTree\FolderTree\obj\Debug\FolderTree.dll”不包含任何 UserControl 类型 c#如何将利用XSLT将dataset.GetXml()返回的XML转换成属性节点样式的XML? 对应的XSLT如何写? [抢分啦]咨询一个可大可小的关于性能的问题 请问外汇数据接口C#源码 在 C#中 怎么应用扫描仪 关于treeview的问题,请各位指教 想做一个服务程序,开机的时候自动启动 求一算法
http://www.cnblogs.com/duguguiyu/archive/2006/08/11/474400.html
public class BiultReportForm { /// /// word 应用对象 /// private Microsoft.Office.Interop.Word.Application _wordApplication; /// /// word 文件对象 /// private Microsoft.Office.Interop.Word.Document _wordDocument; /// /// 创建文档 /// public void CreateAWord() { //实例化word应用对象 this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); Object myNothing = System.Reflection.Missing.Value; this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing); } /// /// 添加页眉 /// /// public void SetPageHeader(string pPageHeader) { //添加页眉 this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView; this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader; this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader); //设置中间对齐 this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; //跳出页眉设置 this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; } /// /// 插入文字 /// /// 文本信息 /// 字体打小 /// 字体颜色 /// 字体粗体 /// 方向 public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment) { //设置字体样式以及方向 this._wordApplication.Application.Selection.Font.Size = pFontSize; this._wordApplication.Application.Selection.Font.Bold = pFontBold; this._wordApplication.Application.Selection.Font.Color= pFontColor; this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment; this._wordApplication.Application.Selection.TypeText(pText); } /// /// 换行 /// public void NewLine() { //换行 this._wordApplication.Application.Selection.TypeParagraph(); } /// /// 插入一个图片 /// /// public void InsertPicture(string pPictureFileName) { object myNothing = System.Reflection.Missing.Value; //图片居中显示 this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing); } /// /// 保存文件 /// /// 保存的文件名 public void SaveWord(string pFileName) { object myNothing = System.Reflection.Missing.Value; object myFileName = pFileName; object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument; object myLockd = false; object myPassword = ""; object myAddto = true; try { this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword, ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing, ref myNothing, ref myNothing, ref myNothing); } catch { throw new Exception("导出word文档失败!"); } } }
{
///
/// word 应用对象
///
private Microsoft.Office.Interop.Word.Application _wordApplication;
///
/// word 文件对象
///
private Microsoft.Office.Interop.Word.Document _wordDocument;
///
/// 创建文档
///
public void CreateAWord()
{ //实例化word应用对象
this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
Object myNothing = System.Reflection.Missing.Value;
this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
}
///
/// 添加页眉
///
///
public void SetPageHeader(string pPageHeader)
{
//添加页眉
this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView;
this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
//设置中间对齐
this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
//跳出页眉设置
this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
}
///
/// 插入文字
///
/// 文本信息
/// 字体打小
/// 字体颜色
/// 字体粗体
/// 方向
public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)
{
//设置字体样式以及方向
this._wordApplication.Application.Selection.Font.Size = pFontSize;
this._wordApplication.Application.Selection.Font.Bold = pFontBold;
this._wordApplication.Application.Selection.Font.Color= pFontColor;
this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;
this._wordApplication.Application.Selection.TypeText(pText);
}
///
/// 换行
///
public void NewLine()
{
//换行
this._wordApplication.Application.Selection.TypeParagraph();
}
///
/// 插入一个图片
///
///
public void InsertPicture(string pPictureFileName)
{
object myNothing = System.Reflection.Missing.Value;
//图片居中显示
this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);
}
///
/// 保存文件
///
/// 保存的文件名
public void SaveWord(string pFileName)
{
object myNothing = System.Reflection.Missing.Value;
object myFileName = pFileName;
object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument;
object myLockd = false;
object myPassword = "";
object myAddto = true;
try
{
this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
ref myNothing, ref myNothing, ref myNothing);
}
catch
{
throw new Exception("导出word文档失败!");
}
}
}