Created
June 22, 2016 06:36
-
-
Save zaconeco/79d2d77a6284460ac5af39ee65dd655e to your computer and use it in GitHub Desktop.
ConvineBooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void ConvineBooks(object sender, RibbonControlEventArgs e) | |
{ | |
// アプリケーション | |
Excel.Application thisApp = Globals.ThisAddIn.Application; | |
// 現在のブック | |
Excel.Workbook thisBook = thisApp.ActiveWorkbook; | |
using (FolderBrowserDialog fbd = new FolderBrowserDialog()) | |
{ | |
if (fbd.ShowDialog() == DialogResult.OK) | |
{ | |
string[] files = Directory.GetFiles(fbd.SelectedPath).Where(x => Path.GetExtension(x) == ".xlsx").ToArray(); | |
foreach (string file in files) | |
{ | |
try | |
{ | |
Excel.Workbook book; | |
// 現在のアプリケーションで開かないと、Copyで死ぬ。 | |
book = (Excel.Workbook)(thisApp.Workbooks.Open(file)); | |
try | |
{ | |
foreach (Excel.Worksheet sheet in book.Sheets) | |
{ | |
// シート[1]の後ろにコピーする | |
sheet.Copy(Type.Missing, thisBook.Worksheets[1]); | |
} | |
} | |
finally | |
{ | |
book.Close(false, Type.Missing, Type.Missing); | |
} | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment