Skip to content

Instantly share code, notes, and snippets.

@zaconeco
Created June 22, 2016 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaconeco/79d2d77a6284460ac5af39ee65dd655e to your computer and use it in GitHub Desktop.
Save zaconeco/79d2d77a6284460ac5af39ee65dd655e to your computer and use it in GitHub Desktop.
ConvineBooks
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