winform导出word问题

dafei198607 2010-10-19 03:51:27
我用c#开发了一个cs系统,需要导出word,已导出成功,但是里面的一些设置弄不明白,比如怎么加table上面的文字,还有怎么让word横向排板,怎么加页眉页脚,把代码贴上来,大家帮我分析一下

string savepath = "";
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
savepath = folderBrowserDialog1.SelectedPath;
}

Object Nothing = System.Reflection.Missing.Value;
object filename = savepath + "/" + "日报";
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

WordDoc.Paragraphs.First.Range.Text = "制表人:" + list[0].Recorder+" " + list[0].RecordTime;
WordDoc.Paragraphs.First.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(WordApp.Selection.Range,list.Count+1 , 14, ref Nothing, ref Nothing);


table.Cell(1, 1).Range.Text = "序号";
table.Cell(1, 2).Range.Text = "航段";
table.Cell(1, 3).Range.Text = "海山";
table.Cell(1, 4).Range.Text = "站号";
table.Cell(1, 5).Range.Text = "采样方式";
table.Cell(1, 6).Range.Text = "经度";
table.Cell(1, 7).Range.Text = "纬度";
table.Cell(1, 8).Range.Text = "水深";
table.Cell(1, 9).Range.Text = "样品号";
table.Cell(1, 10).Range.Text = "岩心长度";
table.Cell(1, 11).Range.Text = "壳层厚度";
table.Cell(1, 12).Range.Text = "基岩厚度";
table.Cell(1, 13).Range.Text = "样品箱号";
table.Cell(1, 14).Range.Text = "备注";
for (int i = 0; i < list.Count; i++)
{
Ocean_ListOfDrill_Sediment temp = list[i];
table.Cell(i + 2, 1).Range.Text = (i+1).ToString();
table.Cell(i + 2, 2).Range.Text = temp.StationNum.LegInfo.Name;
table.Cell(i + 2, 3).Range.Text = temp.StationNum.SeaArea.PositionName;
table.Cell(i + 2, 4).Range.Text = temp.StationNum.SiteCode;
table.Cell(i + 2, 5).Range.Text = temp.StationNum.SamplingStyle.SamplingStyle;
table.Cell(i + 2, 6).Range.Text = temp.StationNum.Longitude.ToString();
table.Cell(i + 2, 7).Range.Text = temp.StationNum.Latitude.ToString();
table.Cell(i + 2, 8).Range.Text = temp.StationNum.WaterDepth;
table.Cell(i + 2, 9).Range.Text = temp.SampleNum;
table.Cell(i + 2, 10).Range.Text = temp.RockLength;
table.Cell(i + 2, 11).Range.Text = temp.ShellThickness;
table.Cell(i + 2, 12).Range.Text = temp.RockThickness;
table.Cell(i + 2, 13).Range.Text = temp.BoxNum;
table.Cell(i + 2, 14).Range.Text = temp.Remark;

}
list.Clear();
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);
MessageBox.Show("导出成功!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
...全文
701 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
chgazhou 2011-09-03
  • 打赏
  • 举报
回复
哪位高人指点一下,,,分栏的代码是什么啊 !!还有中间的线
dafei198607 2010-10-21
  • 打赏
  • 举报
回复
是不是还需要什么设置
dafei198607 2010-10-21
  • 打赏
  • 举报
回复
 WordDoc.Paragraphs.First.Range.Text = "制表人:" + list[0].Recorder.Name+"                    " + list[0].RecordTime;



WordDoc.Paragraphs.First.Range.Text = "头部分:";
object unit = 1;
object count = 1;
unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
WordApp.Selection.EndKey(ref unit, ref Nothing);
WordDoc.Application.Selection.TypeText("测试测试测试 ");

我加在了这,头部是显示了,尾部的不显示了呀,我想头部尾部都显示,谢谢路人甲,
zcty520 2010-10-21
  • 打赏
  • 举报
回复


//在保存之前添加尾部的就可以了啊
//放在 list.Clear();下面

WordDoc.Paragraphs.Last.Range.Text = "尾部部分:";
dafei198607 2010-10-20
  • 打赏
  • 举报
回复
路人甲还在吗?还有个小问题,我在生成的表格下面加了段文字
WordDoc.Paragraphs.First.Range.Text = "制表人:"是这么写的
我想在上面写一段文字,怎么写呀
dafei198607 2010-10-20
  • 打赏
  • 举报
回复
多谢大家帮忙,哦了,呵呵
zcty520 2010-10-20
  • 打赏
  • 举报
回复
//在你的WordDoc 定义下加上以下代码
WordDoc.Paragraphs.First.Range.Text = "头部分:";
object unit = 1;
object count = 1;
unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
WordApp.Selection.EndKey(ref unit, ref Nothing);
WordDoc.Application.Selection.TypeText("测试测试测试 ");
//效果就是以上这些文字在表格上面,不知道是不是楼主想要的效果
wuyq11 2010-10-19
  • 打赏
  • 举报
回复
WApp.ActiveDocument.PageSetup.HeaderDistance
word编程
zcty520 2010-10-19
  • 打赏
  • 举报
回复
还有楼主,你既然导出word表格为何不设置表格边框、网格线这些呢?
//设置边框效果
object style = "网格型";
table.set_Style(ref style);
table.Rows.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleInset;
table.Borders.Shadow = false;
table.Borders.OutsideColor = Microsoft.Office.Interop.Word.WdColor.wdColorRed;
zcty520 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 zcty520 的回复:]
//横向显示
WordApp.ActiveDocument.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientLandscape;
[/Quote]

加到
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
后面
zcty520 2010-10-19
  • 打赏
  • 举报
回复
//横向显示
WordApp.ActiveDocument.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientLandscape;
dafei198607 2010-10-19
  • 打赏
  • 举报
回复
录制宏生成的代码看不懂呀
Sub Macro5()
'
' Macro5 Macro
' 宏在 2010-10-19 由 雨林木风 录制
'
With Selection.Font
.NameFarEast = "黑体"
.NameAscii = "Times New Roman"
.NameOther = "Times New Roman"
.Name = "Times New Roman"
.Size = 10.5
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorAutomatic
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 1
.Animation = wdAnimationNone
.DisableCharacterSpaceGrid = False
.EmphasisMark = wdEmphasisMarkNone
End With
End Sub
menyanjun 2010-10-19
  • 打赏
  • 举报
回复
顶!!!!!!
dafei198607 2010-10-19
  • 打赏
  • 举报
回复
我放错地方了,运行没问题,但是没加上,现在最需要解决的是怎么能改为横向显示呀
dafei198607 2010-10-19
  • 打赏
  • 举报
回复
异常是 “因为没有打开的文档,所以这一命令无效。”
dafei198607 2010-10-19
  • 打赏
  • 举报
回复
加上这一句报异常,录制宏怎么弄呀
zcty520 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zcty520 的回复:]
//页眉设置
//Word.Application WordApp = new Word.ApplicationClass();
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(" [页眉设置] ")
//。。。。。
楼主,具体的,你用在word里录制宏,参照生成的vb代码,在c#参照这写,基本差不多的
[/Quote]
zcty520 2010-10-19
  • 打赏
  • 举报
回复
//页眉设置
Word.Application WordApp = new Word.ApplicationClass();
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(" [页眉设置] ")
//。。。。。
楼主,具体的,你用在word里录制宏,参照生成的vb代码,在c#参照这写,基本差不多的
dafei198607 2010-10-19
  • 打赏
  • 举报
回复
没人来呀,自己顶
dafei198607 2010-10-19
  • 打赏
  • 举报
回复
我顶我顶,我使劲的顶
加载更多回复(2)

110,556

社区成员

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

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

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