操作word的问题,希望高手指点

[复制链接]
查看11 | 回复5 | 2021-1-27 06:21:41 | 显示全部楼层 |阅读模式
1、如何打开一个已经存在的word模版,并向模版中写入文字,并设置文字居中显示?
2、如何进行word文档的页面设置操作

希望高手帮助解决难题,不胜感激!

分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:21:41 | 显示全部楼层
自己顶,期待高手出现
回复

使用道具 举报

千问 | 2021-1-27 06:21:41 | 显示全部楼层
vba实现,
先在word中录制好宏,将其直接修改后(主要修改selectRange),复制到程序中即可
下面是我之前做的一个例子的代码:
'直接打印
PrivateSubcmdPrint_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlescmdSheetPrint.Click
'-1
DimexcelappAsMicrosoft.Office.Interop.Excel.Application'声明一个application对象
DimexcelworkbookAsMicrosoft.Office.Interop.Excel.Workbook'声明一个工作簿对象
DimexcelsheetAsMicrosoft.Office.Interop.Excel.Worksheet'声明一个工作表
excelapp=NewMicrosoft.Office.Interop.Excel.Application
'excelapp.Visible=True
excelworkbook=excelapp.Workbooks.Open(My.Application.Info.DirectoryPath+"\调度单模板.xlsx")'访问到工作簿:绝对路径
excelsheet=excelworkbook.Sheets.Item(1)'访问到工作表:item使用索引值来得到sheet对象的引用
excelsheet.Activate()
''注入数据
''excelsheet.Cells(row,col)=值
'--1.1
excelsheet.Cells(2,2)=TxtProName.Text.Trim'B2产品名称
excelsheet.Cells(2,6)=TxtPlanNo.Text.Trim'F2计划单号
excelsheet.Cells(2,9)=TxtCusName.Text.Trim'I2客户名
excelsheet.Cells(2,13)=DtpDate.Text.Trim'M2订单生成日期
excelsheet.Cells(3,2)=CbxDepartment.Text.Trim'B3部门
excelsheet.Cells(3,4)=CbxClass.Text.Trim'D3班组
excelsheet.Cells(3,7)=TxtPlanAsemb.Text.Trim'G3计划装配数
excelsheet.Cells(3,9)=TxtfacAsemb.Text.Trim'I3实际装配数
excelsheet.Cells(3,13)=DtpPlanDate.Text.Trim'M3计划交期
'/--1.1
'--1.2
DimrowCountAsInteger=DgvObjScan.RowCount'dgv中有多少条数据
DimcurentRowAsInteger=-1'读取dgv的当前的行数,从0开始
DimexcelRowAsInteger=4'注入到EXCEL中的行,模板中从第五行开始
DimrowAsInteger'循环控制
Forrow=1TorowCount
curentRow=curentRow+1'读取dgv下一行
If(curentRow+1)Mod20Then'dgv中奇数行curentRow=0,2,4,6,8.......
excelRow=4+(curentRow+2)/2
excelsheet.Cells(excelRow,1)=DgvObjScan.Rows(curentRow).Cells(1).Value
excelsheet.Cells(excelRow,2)=DgvObjScan.Rows(curentRow).Cells(2).Value
excelsheet.Cells(excelRow,4)=DgvObjScan.Rows(curentRow).Cells(3).Value
excelsheet.Cells(excelRow,5)=DgvObjScan.Rows(curentRow).Cells(4).Value
Else'dgv中偶数行curentRow=1,2,3,7,9.......
excelRow=4+(curentRow+1)/2
excelsheet.Cells(excelRow,7)=DgvObjScan.Rows(curentRow).Cells(1).Value
excelsheet.Cells(excelRow,8)=DgvObjScan.Rows(curentRow).Cells(2).Value
excelsheet.Cells(excelRow,10)=DgvObjScan.Rows(curentRow).Cells(3).Value
excelsheet.Cells(excelRow,11)=DgvObjScan.Rows(curentRow).Cells(4).Value
EndIf
Next
'/--1.2
'--1.3
excelsheet.Cells(20,2)=TxtPrint.Text.Trim'B20打字
excelsheet.Cells(21,2)=TxtPack1.Text.Trim'B21包装1
excelsheet.Cells(22,2)=TxtPack2.Text.Trim'B22包装2
excelsheet.Cells(23,2)=TxtPack3.Text.Trim'B23包装3
IfCInt(TxtPACKAGE_ODD.Text.Trim)0Then
excelsheet.Cells(23,14)=TxtPACKAGE_ODD.Text.Trim'J24零头
EndIf
IfCInt(TxtPACKAGE_SET.Text.Trim)0Then
excelsheet.Cells(23,10)=TxtPACKAGE_SET.Text.Trim'J23套数
EndIf
IfCInt(TxtSET_AMOUNT.Text.Trim)0Then
excelsheet.Cells(24,12)=TxtSET_AMOUNT.Text.Trim'总套数
EndIf

excelsheet.Cells(24,2)=TxtRmk1.Text.Trim'备注1
excelsheet.Cells(25,2)=TxtRmk2.Text.Trim'备注2
excelsheet.Cells(25,13)=DtpStorDte.Text.Trim'入库日期
excelsheet.Cells(26,2)=TxtISPATCHER_NAME.Text.Trim'
excelsheet.Cells(26,5)=TxtSTORAGER_NAME.Text.Trim'
excelsheet.Cells(26,8)=TxtQA_NAME.Text.Trim'
excelsheet.Cells(26,11)=TxtHANDER_OVER_NAME.Text.Trim'
excelsheet.PrintOut()'直接打印该工作簿
'/--1.3
'设置文件保存位置
DimfilPassAsString=g_str_filePath+"\"+TxtPlanNo.Text.Trim+"_"+TxtProName.Text.Trim+"_"+CbxClass.Text.Trim+"调度单.xlsx"
'--1.4
'如果系统开启该功能
Ifg_bool_isSheetSaveAsEXL=TrueThen'保存
excelworkbook.SaveAs(filPass)
Ifg_bool_isFileSavePoints=TrueThen
MessageBox.Show("导出成功,存在该目录下:"+filPass,"OK",MessageBoxButtons.OK,MessageBoxIcon.Information)
EndIf
Else
excelworkbook.SaveAs(filPass)
excelsheet=Nothing
excelworkbook.Close()
excelworkbook=Nothing
excelapp.Quit()
excelapp=Nothing
'需要先释放再删除
IO.File.Delete(filPass)
ExitSub
EndIf
'/--1.4
'释放
excelsheet=Nothing
excelworkbook.Close()
excelworkbook=Nothing
excelapp.Quit()
excelapp=Nothing
'/-1
EndSub

这是先打开模板,再在其指定的cell中写数据,我这里面的数据是从界面中读取的
回复

使用道具 举报

千问 | 2021-1-27 06:21:41 | 显示全部楼层
这个是个C#的代码,看不明白,有没有高手指点一下,用vb.net解决以上问题,在线期待ing。。。。
回复

使用道具 举报

千问 | 2021-1-27 06:21:41 | 显示全部楼层
再顶起来,感谢所有能帮助我的人,希望高手给指点下吧。。。
回复

使用道具 举报

千问 | 2021-1-27 06:21:41 | 显示全部楼层
快来人吧,给我看看怎么解决问题吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行