C#을 사용한 Excel Automation에서 상행 프리즈 및 필터 적용 방법
C#에서 엑셀 문서를 작성하는 자동화가 되어 있습니다.워크시트 맨 위 행을 고정하고 필터를 적용하려고 합니다.이는 Excel 2010에서 [View]> [ Freeze Panes ]> [ Freeze top row ]를 선택한 후 맨 위 행의 [Data]> [ Filter ]를 선택한 경우와 동일합니다.필터 적용 방법을 잘 모르겠습니다만, 맨 위 행의 프리즈를 시도했더니, 워크시트 전체가 프리즈 되었습니다.누가 내 문제에 대한 해결책을 가지고 있나요?데이터 필터 문제는 제가 도움이 더 필요한 부분이기 때문에, 그것에 대한 해결책을 가지고 있는 사람이 있으면 가르쳐 주세요.
KBP님 감사합니다.
workSheet.Activate();
Excel.Range firstRow = (Excel.Range)workSheet.Rows[1];
firstRow.Activate();
firstRow.Select();
firstRow.Application.ActiveWindow.FreezePanes = true;
내가 알아냈어!
@Jaime의 맨 윗줄 동결 솔루션은 완벽하게 작동했다.필터 적용 방법은 다음과 같습니다.
KBP님 감사합니다
// Fix first row
workSheet.Activate();
workSheet.Application.ActiveWindow.SplitRow = 1;
workSheet.Application.ActiveWindow.FreezePanes = true;
// Now apply autofilter
Excel.Range firstRow = (Excel.Range)workSheet.Rows[1];
firstRow.AutoFilter(1,
Type.Missing,
Excel.XlAutoFilterOperator.xlAnd,
Type.Missing,
true);
이거 드셔보세요.
workSheet.Activate();
workSheet.Application.ActiveWindow.SplitRow = 1;
workSheet.Application.ActiveWindow.FreezePanes = true;
workSheet.EnableAutoFilter = true;
workSheet.Cells.AutoFilter(1);
//Set the header-row bold
workSheet.Range["A1", "A1"].EntireRow.Font.Bold = true;
//Adjust all columns
workSheet.Columns.AutoFit();
있을 수 있어System.Reflection.Missing.Value
인수로 넘어가야 하는데 이게 VB였어요.내 마음속에서 변환한 인터넷 코드.
아래의 솔루션은 정상적으로 동작하고 있습니다만, 시트의 현재 표시 가능한 스냅샷의 첫 번째 행은 정지하고 있습니다.예: 현재 시트 표시 스냅샷이 43행의 스냅샷인 경우.freeze row는 43에 적용됩니다.
시트 맨 앞줄(제목줄)만 동결하고 싶다면 엑셀 스크롤 위치에 관계없이 아래 솔루션이 효과가 있었습니다.이 코드는 엑셀 시트를 1행까지 스크롤합니다.프리즈 전 이전 위치로 돌아가려면 위치를 저장해야 합니다.
worksheet.Application.ActiveWindow.ScrollRow = 1;
worksheet.Application.ActiveWindow.SplitRow = 1;
worksheet.Application.ActiveWindow.FreezePanes = true;
//path were excel 파일은 문자열 ResultsFilePath = @"C:\Users\krakhil\데스크탑\폴더명\파일 이름 확장자 없음";
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelWorkbook = ExcelApp.Workbooks.Open(ResultsFilePath);
ExcelApp.Visible = true;
//Looping through all available sheets
foreach (Excel.Worksheet ExcelWorksheet in ExcelWorkbook.Sheets)
{
//Selecting the worksheet where we want to perform action
ExcelWorksheet.Select(Type.Missing);
//Making sure first row is selected - else split and freeze will happen
//On the visible part and not from the top
Excel.Range activeCell = ExcelWorksheet.Cells[1, 1];
activeCell.Select();
//Applying auto filter to Row 10
activeCell = (Excel.Range)ExcelWorksheet.Rows[10];
activeCell.AutoFilter(1,
Type.Missing,
Excel.XlAutoFilterOperator.xlAnd,
Type.Missing,
true);
//Split the pane and freeze it
ExcelWorksheet.Application.ActiveWindow.SplitRow = 10;
ExcelWorksheet.Application.ActiveWindow.FreezePanes = true;
//Auto fit all columns
ExcelWorksheet.Columns.AutoFit();
//Releasing range object
Marshal.FinalReleaseComObject(activeCell);
}
//saving excel file using Interop
ExcelWorkbook.Save();
//closing file and releasing resources
ExcelWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(ExcelWorkbook);
ExcelApp.Quit();
Marshal.FinalReleaseComObject(ExcelApp);
언급URL : https://stackoverflow.com/questions/5060488/how-to-freeze-top-row-and-apply-filter-in-excel-automation-with-c-sharp
'programing' 카테고리의 다른 글
앱이 백그라운드에서 돌아왔을 때 viewApp이 호출되지 않는 이유는 무엇입니까? (0) | 2023.04.15 |
---|---|
디렉토리내의 파일의 경우, 에코 파일명(경로 없음)만을 사용합니다. (0) | 2023.04.15 |
git: 치명적:원격 저장소에서 읽을 수 없습니다. (0) | 2023.04.15 |
#!/usr/bin/env bash가 #!/bin/bash보다 우월한 이유는 무엇입니까? (0) | 2023.04.15 |
Python 3에서 파일 내용을 처리할 때 "TypeError: "str"이 아닌 바이트와 유사한 개체가 필요합니다." (0) | 2023.04.15 |