如何用c#程序读取word中表格的属性

wangyz_ecc 2009-05-08 09:49:23
如题
在网上找了很多资料都只有读取word表格的内容的程序,并且这些程序对合并后的表格都不适用了。
我想的是不仅能读出里面的数据信息,还要把这个表格的所有属性都读出来,包括表格的列宽,行宽,边宽和底纹,线条粗细,文字方向、大小和字体,表格指定宽度,每个单元格的对齐方式……等等,以便对其进行Hash。
望高手指点一下,给一些这方面的资料,越全越好,非常谢谢谢谢!!
好心人有资料的话可发到我的邮箱一下:wangyz_ecc@163.com 再次非常特别十分感谢!
...全文
2581 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
青春扯蛋 2010-08-23
  • 打赏
  • 举报
回复
这有啥子用?
ndscwsy 2010-04-18
  • 打赏
  • 举报
回复
我也急需解决这个问题,那位能帮帮,表格我已经能顺利读取,但是读不出不规则表格,如合并过的单元格,和拆分的单元格,怎样判断纵向合并和横向合并,请教了各位,邮件ndscwsy@tom.com,qq39399373
春天的气息 2009-05-24
  • 打赏
  • 举报
回复
看一下这个贴子吧,老外的,有案例下载:http://www.bbs180.com/showtopic-583-1.aspx#1195

悔说话的哑巴 2009-05-24
  • 打赏
  • 举报
回复
帮顶,代码有点多。
十八道胡同 2009-05-24
  • 打赏
  • 举报
回复
帮顶~
蓝海D鱼 2009-05-08
  • 打赏
  • 举报
回复
把datagridview的数据输出为Excel,Word的简单应用
Posted by 上来下去
public static void ExportData(DataGridView srcDgv,string fileName)//导出数据,传入一个datagridview和一个文件路径
{
string type = fileName.Substring(fileName.IndexOf(”.”)+1);//获得数据类型
if (type.Equals(”xls”,StringComparison.CurrentCultureIgnoreCase))//Excel文档
{
Excel.Application excel = new Excel.Application();
try
{
excel.DisplayAlerts = false;
excel.Workbooks.Add(true);
excel.Visible = false;

for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
{
excel.Cells[2, i+1] = srcDgv.Columns[i].HeaderText;
}

for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{
excel.Cells[i + 3, j + 1] = srcDgv[j, i].Value;
}
}

excel.Workbooks[1].SaveCopyAs(fileName);//保存
}
finally
{
excel.Quit();
}
return;
}

//保存Word文件

if (type.Equals(”doc”, StringComparison.CurrentCultureIgnoreCase))
{

object path = fileName;

Object none=System.Reflection.Missing.Value;
Word.Application wordApp = new Word.Application();
Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
//建立表格
Word.Table table= document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count+1, srcDgv.Columns.Count, ref none, ref none);

try
{

for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
{
table.Cell(1, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
}

for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{

table.Cell(i + 2, j + 1).Range.Text = srcDgv[j, i].Value.ToString();
}
}

document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
document.Close(ref none, ref none, ref none);

}
finally
{
wordApp.Quit(ref none, ref none, ref none);
}

}

}
uncleson88 2009-05-08
  • 打赏
  • 举报
回复
牛人~~~~这些VBA帮助中都有啊~~~帮助的使用方法同MSDN :P
wangyz_ecc 2009-05-08
  • 打赏
  • 举报
回复
自己顶一下
继续等待回答……
wangyz_ecc 2009-05-08
  • 打赏
  • 举报
回复
非常感谢!
不过这些我在网上都看到过,并且我要的不是对表格的操作,而是读取表格的所有属性。不过还是非常谢谢你!
fengrx 2009-05-08
  • 打赏
  • 举报
回复
VBA帮助中都有,我把Word的VBA帮助文档发到你的邮箱了。
knotchen 2009-05-08
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;

namespace WordTest
{
class CreateAndReadWord
{
//下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:

public string CreateWordFile(string CheckedInfo)
{
string message;
string sPicturePath = @"C:\Documents and Settings\All Users\Documents\My Pictures\示例图片\Blue hills.jpg";
try
{
Object Nothing = System.Reflection.Missing.Value;
//创建文件所在目录
Directory.CreateDirectory(@"D:\work\20090407\WordTest\Test");
string name = "Test" + DateTime.Now.ToShortDateString() + ".doc";
//文件保存路径
object filename = @"D:\work\20090407\WordTest\Test" + name;
//创建Word文档
Application WordApp = new ApplicationClass();
Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

//添加页眉
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; //设置右对齐
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; //跳出页眉设置
WordApp.Selection.ParagraphFormat.LineSpacing = 15f; //设置文档的行间距

//移动焦点并换行
object count = 14;
object WdLine = WdUnits.wdLine; //换一行;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing); //移动焦点
WordApp.Selection.TypeParagraph(); //插入段落

//文档中创建表格
Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
//设置表格样式
newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 220f;
newTable.Columns[3].Width = 105f;

//填充表格内容
newTable.Cell(1, 1).Range.Text = "产品详细信息表";
newTable.Cell(1, 1).Range.Bold = 2; //设置单元格中字体为粗体
//合并单元格
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; //垂直居中
WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; //水平居中

//填充表格内容
newTable.Cell(2, 1).Range.Text = "产品基本信息";
newTable.Cell(2, 1).Range.Font.Color = WdColor.wdColorDarkBlue; //设置单元格内字体颜色
//合并单元格
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;

//填充表格内容
newTable.Cell(3, 1).Range.Text = "品牌名称:";

newTable.Cell(3, 2).Range.Text = "BrandName";
//纵向合并单元格
newTable.Cell(3, 3).Select(); //选中一行
object moveUnit = WdUnits.wdLine;
object moveCount = 5;
object moveExtend = WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();
//插入图片
string FileName = sPicturePath; //图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = WordDoc.Application.Selection.Range;
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile,
ref SaveWithDocument, ref Anchor);
WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f; //图片宽度
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f; //图片高度
//将图片设置为四周环绕型
Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = WdWrapType.wdWrapSquare;

newTable.Cell(12, 1).Range.Text = "产品特殊属性";
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
//在表格中增加行
WordDoc.Content.Tables[1].Rows.Add(ref Nothing);

WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now; //“落款”
WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphRight;

//文件保存
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
message = name + "文档生成成功,以保存到C:CNSI下";
}
catch
{
message = "文件导出异常!";
}
return message;
}
}
}

这份希望能有帮助!

110,535

社区成员

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

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

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