如何使用 .NET 将 Excel 中的格式化单元格复制到 Word 中的表格单元格?

发布于 2024-08-29 15:37:28 字数 230 浏览 2 评论 0原文

我正在尝试将单元格(一次一个)从 Excel 2003(或 2007)电子表格复制到 Word 2003(或 2007)表格。我希望代码与版本无关,因此我使用后期绑定。需要保留 Excel 单元格内容的格式,例如颜色、下划线、删除线。我的方法是使用Word文档作为模板。它的顶部有一个表格,我可以将其复制到文档末尾,根据需要添加行,并使用 Excel 电子表格中的数据填充 Word 表格单元格。不幸的是,所有格式都消失了。我得到的只是文本本身。

I'm attempting to copy cells, one at a time, from an Excel 2003 (or 2007) spreadsheet to a Word 2003 (or 2007) table. I'd like the code to be version-agnostic, and so am using late binding. The formatting of the contents of the Excel cell, such as color, underline, strike-through, needs to be preserved. My approach is to use a Word doc as a template. It has a table at the top which I can copy to the end of the doc, add rows as needed, and fill in the word table cells with the data from the excel spreadsheet. Unfortunately, all the formatting disappears. All I get is the text itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2024-09-05 15:37:28

想通了。事实证明比预想的要容易。

Dim appExcel As Excel.Application
Dim thisWorkbook As Excel.Workbook
Dim thisWorksheet As Excel.Worksheet

appExcel = CType(CreateObject("Excel.Application"), Excel.Application)
thisWorkbook = appExcel.Workbooks.Open("Excelfilename.xls")
thisWorksheet = CType(thisWorkbook.ActiveSheet, Excel.Worksheet)

Dim appWord As Word.Application
Dim thisDoc As Word.Document
Dim thisWordTable As Word.Table

' Use a template word doc that has a table in it.

appWord = CType(CreateObject("Word.Application"), Word.Application)
thisDoc = appWord.Documents.OpenNoRepairDialog("templateWordFileName.doc")
thisDoc.SaveAs("outputDocFileName.doc")

' Get a reference to the table.
thisWordTable = thisDoc.Tables(0)

' Copy data from excel to this table.
CType(thisWorksheet.Cells(5, 1), Excel.Range).Copy()
thisWordTable.Cell(1, 2).Select()
appWord.Selection.Paste()

thisDoc.Save()
thisDoc.Close()
appWord.Quit()

thisWorkbook.Close(False)   ' Don't save any changes to workbook.
appExcel.Quit()

Figured it out. Turns out to be easier than expected.

Dim appExcel As Excel.Application
Dim thisWorkbook As Excel.Workbook
Dim thisWorksheet As Excel.Worksheet

appExcel = CType(CreateObject("Excel.Application"), Excel.Application)
thisWorkbook = appExcel.Workbooks.Open("Excelfilename.xls")
thisWorksheet = CType(thisWorkbook.ActiveSheet, Excel.Worksheet)

Dim appWord As Word.Application
Dim thisDoc As Word.Document
Dim thisWordTable As Word.Table

' Use a template word doc that has a table in it.

appWord = CType(CreateObject("Word.Application"), Word.Application)
thisDoc = appWord.Documents.OpenNoRepairDialog("templateWordFileName.doc")
thisDoc.SaveAs("outputDocFileName.doc")

' Get a reference to the table.
thisWordTable = thisDoc.Tables(0)

' Copy data from excel to this table.
CType(thisWorksheet.Cells(5, 1), Excel.Range).Copy()
thisWordTable.Cell(1, 2).Select()
appWord.Selection.Paste()

thisDoc.Save()
thisDoc.Close()
appWord.Quit()

thisWorkbook.Close(False)   ' Don't save any changes to workbook.
appExcel.Quit()
神妖 2024-09-05 15:37:28

您可能必须先编写文本,然后手动复制样式。您可以使用 Workbook.Styles 获取 Microsoft.Office.Interop.Excel.Style 的集合,然后应该能够使用该数据创建 Microsoft.Office.Interop.Word.Styles。

You might have to do the text first and then copy the style manually. You can use Workbook.Styles to get the collection of Microsoft.Office.Interop.Excel.Style and should then be able to create Microsoft.Office.Interop.Word.Styles using that data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文