如何使用C#编写Wordx,Excelx,PPTx文件 [英] How to Wordx, Excelx, PPTx file using c#

查看:171
本文介绍了如何使用C#编写Wordx,Excelx,PPTx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我遇到一种情况,需要保存在项目中打开的当前活动文档.例如当我打开Word或Excel文件时,我将对同一文档进行一些更改.现在我要保存文档.

适用于Excel



I have a situation where I need to save current active document opened with my project. e.g. When I open Word or Excel file and I will do some changes into the same document. Now I want to save the document.

for Excel

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

//This will gives you the Active work book name
//excelApp.Application.ActiveWorkbook.Name;

//This will save the original document...
excelApp.ActiveWorkbook.Save();


在上面的代码中,我没有得到准确的活动工作簿,因此无法正确保存它.它显示的是空的活动工作簿.

还有什么其他方法可以保存已打开的DOCX,ELSX&使用C#的PPTX文档?

谢谢&问候,
Balkrishna Raut


in above code I am not getting the exact active workbook so that I can save it properly. It is showing null active workbook.

Is there any other way to save already opened DOCX, ELSX & PPTX document using C#?

Thanks & Regards,
Balkrishna Raut

推荐答案

// Create Excel app
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

// Open Excel file
excelApp.Workbooks.Open("c:\\test\\test.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

// Find the active sheet
Microsoft.Office.Interop.Excel.Worksheet sheet = excelApp.ActiveWorkbook.ActiveSheet;

// Get a range to modify
Microsoft.Office.Interop.Excel.Range range = sheet.get_Range("A1", Type.Missing);

// Set a value for this range
range.set_Value(Type.Missing, "test value");

// Close and save the file
excelApp.ActiveWorkbook.Close(true, Type.Missing, Type.Missing);

// Close Excel
excelApp.Quit();




"DizZ"回答了您的问题,但我想补充一点.也许有时候对您有用. ;)

寻找OpenXML SDK!这是下载链接: http://www.microsoft.com/downloads/details.aspx?FamilyId = C6E744E5-36E9-45F5-8D8C-331DF206E0D0 [ 使用它不会.
与使用Office(excel单词)互操作性相比,使用它的生活要容易得多. :-D
Hi,

"DizZ" answered to your question, but I want to add something. Maybe it will be useful to you sometimes. ;)

Look for OpenXML SDK! Here is the download link: http://www.microsoft.com/downloads/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0[^]

With it you can do what ever you want with any office document (docx,xlsx, pptx) in "OOP" way.
Using it you don''t .
Working with it life is so much easier then using Office (excel word) interoperability. :-D


这篇关于如何使用C#编写Wordx,Excelx,PPTx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆