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

本文共 2105 字,大约阅读时间需要 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();

转载地址:http://fdrxl.baihongyu.com/

你可能感兴趣的文章
Eclipse通过集成svn实现版本控制
查看>>
OS开发过程中常用开源库
查看>>
关于在多个UItextield切换焦点
查看>>
hdu 2768
查看>>
git记住用户名密码
查看>>
ElasticSearch(2)-安装ElasticSearch
查看>>
从mysql数据表中随机取出一条记录
查看>>
ORACLE 锁表处理,解锁释放session
查看>>
深海机器人问题
查看>>
正则表达式(括号)、[中括号]、{大括号}的区别小结
查看>>
88.NODE.JS加密模块CRYPTO常用方法介绍
查看>>
java.net.ProtocolException: Exceeded stated content-length of: '13824' bytes
查看>>
asp.net 连接 oracle10g 数据库
查看>>
C 入门 第十一节
查看>>
HTML简单的注册页面搭建
查看>>
【06】Vue 之 组件化开发
查看>>
Docker 安装
查看>>
多数据库数据导入
查看>>
[AVR]高压并行编程---基础知识
查看>>
inl文件介绍
查看>>