• 20180518VSTO多簿单表汇总


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Tools.Ribbon;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    using System.IO;
    using System.Diagnostics;
    using System.Windows.Forms;
    
    namespace GatherDataTool
    {
        public partial class Ribbon1
        {
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
    
            }
    
            private void ButtonGather_Click(object sender, RibbonControlEventArgs e)
            {
                TimeSpan StartTime = new TimeSpan(DateTime.Now.Ticks);    
                GatherDataTool.ThisWorkbook wb = Globals.ThisWorkbook;
                Excel.Worksheet sht =(Excel.Worksheet)wb.ActiveSheet;
                sht.Cells.Clear();
                Excel.Application xlApp = wb.Application;
                xlApp.ScreenUpdating = false;
                xlApp.DisplayAlerts = false;
                Excel.Workbook openWb;
                Excel.Worksheet openSht;
                Excel.Range rng;
                int index = 0;
                Office.FileDialog fd = xlApp.FileDialog[Office.MsoFileDialogType.msoFileDialogFolderPicker];
                fd.InitialFileName = xlApp.ActiveWorkbook.Path;
                if(fd.Show()==-1)
                {
                    string folderPath = fd.SelectedItems.Item(1);
                    string[] filePaths = Directory.GetFiles(folderPath, "*.xls*");
                    foreach(string filepath in filePaths)
                    {
                        if(filepath!=wb.FullName)
                        {
                            // Debug.Print(filepath);
                            index++;
                            openWb = xlApp.Workbooks.Open(filepath);
                            openSht = openWb.Worksheets[1];
                            long endrow = openSht.Cells[openSht.Rows.Count,3].End(Excel.XlDirection.xlUp).Row;
                            if (index == 1)
                            {
                                rng = openSht.Range[openSht.Cells[1, 1], openSht.Cells[endrow, 17]];
                                rng.Copy(sht.Cells[1,1]);
                            }
                            else
                            {
                                rng = openSht.Range[openSht.Cells[3, 1], openSht.Cells[endrow, 17]];
                                long nextRow=sht.Cells[sht.Rows.Count,1].End(Excel.XlDirection.xlUp).Row+1;
                                rng.Copy(sht.Cells[nextRow, 1]);
                            }
                            openWb.Close(false, Type.Missing, Type.Missing);
                        }
                    }
                }
    
                xlApp.ScreenUpdating = true;
                xlApp.DisplayAlerts = true;
                TimeSpan EndTime = new TimeSpan(DateTime.Now.Ticks);
                TimeSpan duration = StartTime.Subtract(EndTime).Duration();
    
                MessageBox.Show("汇总完成,耗时:" + duration.Seconds.ToString()+"秒!");
            }
        }
    }
    

      

  • 相关阅读:
    Angular 双向数据绑定
    Angular 过滤器
    Angular 自定义指令传参
    润滑油 标号
    Oracle concat
    sqlldr load UTF8 error
    linux中shell变量$#,$@,$0,$1,$2的含义解释
    shell 执行结果赋给变量
    linux 如何显示一个文件的某几行(中间几行)
    linux shell date 用当天时间做备份文件名
  • 原文地址:https://www.cnblogs.com/nextseven/p/9057858.html
Copyright © 2020-2023  润新知