博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Word Excel 操作总结
阅读量:5111 次
发布时间:2019-06-13

本文共 2157 字,大约阅读时间需要 7 分钟。

1.与office无关使用 Aspose.Cells.dll,Aspose.Words.dll

2.使用Microsoft.Office.Interop.Excel Microsoft.Office.Interop.Word

3.打开文件

WORD:

object oMissing = Missing.Value;

_Application app = new Application();
_Document currentDoc = null;
app.Visible = false;
currentDoc = app.Documents.Open(filePath, ref oMissing, ref oMissing, ref oMissing);

//currentDoc = app.Documents.Add(templatesDirectory,ref oMissing, ref oMissing, ref oMissing); //打开模版

EXCEL:

var app = new Application();

 app.Visible = false;
var workbooks = app.Workbooks;
var workbook = workbooks.Open(filePath);

//var workbook = workbooks.Add(templatePath); //打开模版

var sheets = workbook.Sheets;
var sheet = (_Worksheet)sheets.get_Item(1);

4.保存或另存文件

Excel:

app.DisplayAlerts = false;

            app.ActiveWorkbook.SaveAs(filePath,
                        XlFileFormat.xlWorkbookNormal,
                        Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                        XlSaveAsAccessMode.xlExclusive,
                        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

WORD:

            object oMissing = Missing.Value;

            object oFileName = fileName;
            object oFormat = WdSaveFormat.wdFormatDocument;
            object oAddToRecentFiles = false;
            currentDoc.SaveAs(
                    ref oFileName, ref oFormat, ref oMissing, ref oMissing, ref oAddToRecentFiles,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

5.退出文件:

WORD:

                object oSave = false;

                currentDoc.Close(ref oSave, ref oMissing, ref oMissing);
                Object nothing = Missing.Value;
                app.Quit(ref nothing, ref nothing, ref nothing); 

Excel:

        [DllImport("User32.dll", CharSet = CharSet.Auto)]

        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

 

            IntPtr t = new IntPtr(app.Hwnd);

            int k = 0;
            GetWindowThreadProcessId(t, out k);
            Process p = Process.GetProcessById(k);
            p.Kill();

 6.word 复制表格,插入分页符并黏贴

currentDoc.Tables[1].Select();

                app.Selection.Copy();

object mymissing = Missing.Value;                    

object myunit = WdUnits.wdStory;                    

app.Selection.EndKey(ref myunit, ref mymissing);                    

object pBreak = (int)WdBreakType.wdPageBreak;                    

app.Selection.InsertBreak(ref pBreak);

 

                    app.Selection.Paste();

转载于:https://www.cnblogs.com/xh831213/p/3904763.html

你可能感兴趣的文章
STM32单片机使用注意事项
查看>>
swing入门教程
查看>>
好莱坞十大导演排名及其代表作,你看过多少?
查看>>
Loj #139
查看>>
StringBuffer是字符串缓冲区
查看>>
hihocoder1187 Divisors
查看>>
Azure 托管镜像和非托管镜像对比
查看>>
js window.open 参数设置
查看>>
032. asp.netWeb用户控件之一初识用户控件并为其自定义属性
查看>>
Ubuntu下安装MySQL及简单操作
查看>>
前端监控
查看>>
clipboard.js使用方法
查看>>
移动开发平台-应用之星app制作教程
查看>>
leetcode 459. 重复的子字符串(Repeated Substring Pattern)
查看>>
伪类与超链接
查看>>
centos 7 redis-4.0.11 主从
查看>>
博弈论 从懵逼到入门 详解
查看>>
永远的动漫,梦想在,就有远方
查看>>
springboot No Identifier specified for entity的解决办法
查看>>
慵懒中长大的人,只会挨生活留下的耳光
查看>>