zoukankan      html  css  js  c++  java
  • Word Excel 操作总结

    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();

  • 相关阅读:
    2018-2019-2 网络对抗技术 20165321 Exp3 免杀原理与实践
    2018-2019-2 网络对抗技术 20165321 Exp2 后门原理与实践
    2018-2019-2 网络对抗技术 20165321 Exp1 PC平台逆向破解
    2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165321
    2017-2018-1 JAVA实验站 冲刺 day07
    2017-2018-1 JAVA实验站 冲刺 day06
    2017-2018-1 JAVA实验站 冲刺 day05
    2017-2018-1 JAVA实验站 冲刺 day04
    2017-2018-1 JAVA实验站 冲刺 day03
    2017-2018-1 JAVA实验站 冲刺 day02
  • 原文地址:https://www.cnblogs.com/xh831213/p/3904763.html
Copyright © 2011-2022 走看看