C# Windows 응용 프로그램에서 나는 많은 Excel 통합 문서를 가지고 있는데, 원하는 것은 Excel 통합 문서의 워크 시트를 단일 통합 문서로 복사하는 것입니다. 이 작업은 가능하지만 Excel 통합 문서를 열어야합니다.Excel 워크 시트를 복사하는 방법 C# winforms에서 Excel 파일을 열지 않고 다른 Excel 통합 문서에?
Excel.Application app = new Excel.Application();
app.Visible = true;
app.WindowState = XlWindowState.xlMinimized;
app.Workbooks.Add("");
app.Workbooks.Add(@"Path\WorkBook1.xlsx");
app.Workbooks.Add(@"Path\WorkBook2.xlsx");
for (int i = 2; i <= app.Workbooks.Count; i++)
{
int count = app.Workbooks[i].Worksheets.Count;
app.Workbooks[i].Activate();
for (int j = 1; j <= count; j++)
{
Excel._Worksheet ws = (Excel._Worksheet)app.Workbooks[i].Worksheets[j];
ws.Select(true);
ws.Cells.Select();
Excel.Range sel = (Excel.Range)app.Selection;
sel.Copy(Type.Missing);
Excel._Worksheet sheet = (Excel._Worksheet)app.Workbooks[1].Worksheets.Add(
Type.Missing, Type.Missing, Type.Missing, Type.Missing
);
sheet.Paste(Type.Missing, Type.Missing);
sheet.Name = app.Workbooks[i].Worksheets[j].Name;
}
}
app.DisplayAlerts = false;
app.Workbooks[3].Close();
app.Workbooks[2].Close();
app.DisplayAlerts = true;
Cursor = Cursors.Default;
MessageBox.Show("Successfully Generated Excel...!", "Excel Tool", MessageBoxButtons.OK, MessageBoxIcon.Information);
엑셀 시트를 열지 않고도 모든 데이터를 스타일로 복사 할 수 있습니까?
Excel 파일을 열지 않고 통합 문서에서 단일 시트를 복사 할 수 없습니다. 그러나 복사 작업을 백그라운드에서 실행하려면 별도의 Excel 응용 프로그램에서 복사 작업을 수행하면됩니다. 한 번은 시작 매크로가 포함 된 템플릿 시트를 복사해야했습니다. 매크로가 해제 된 별도의 (보이지 않는) Excel 응용 프로그램에서이 템플릿을 열어야했습니다. – StuartRedmann