이 가이드에서는 C#**에서 **Excel 테이블을 생성하는 방법에 대한 세부 정보를 공유합니다. 이 문서에는 개발용 IDE 설정에 대한 세부 정보, 프로그래밍 작업 목록 및 Microsoft Excel in C# 테이블 생성을 위한 샘플 코드가 포함되어 있습니다. 테이블을 사용자 정의하고 출력을 다양한 형식으로 저장하는 다양한 옵션을 배우게 됩니다.
C#에서 Excel 테이블을 만드는 단계
- Aspose.Cells for .NET을 사용하여 테이블을 생성하도록 IDE를 설정합니다.
- Excel 파일을 만들거나 로드하고 데이터를 사용할 수 있는 sheet에 액세스하세요.
- 데이터가 포함된 셀 범위를 제공하여 목록 개체 만들기
- 목록 개체의 TableStyleType을 TableStyleMedium10으로 설정합니다.
- 모든 숫자 열에 대해 합계 플래그를 true로 설정합니다.
- 두 번째 열에 대한 총 계산을 추가합니다.
- 출력 저장
위 단계에서는 C#*에서 *간단한 Excel 테이블을 만드는 프로세스를 설명합니다. Excel 파일을 로드하거나 생성하고, 시트에 액세스하고, 데이터가 있는 셀 범위가 포함된 목록 개체를 추가하여 프로세스를 시작합니다. 테이블을 참조하는 새 목록 개체를 사용하여 TableStyleType, 플래그를 설정하여 총계 및 계산 유형 개수를 표시합니다.
C#에서 Microsoft Excel 테이블 만들기용 코드
using System; | |
using System.Linq; | |
using Aspose.Cells; | |
using Aspose.Cells.Tables; | |
class Program | |
{ | |
static void Main(string[] args) // Table creation in C# | |
{ | |
new License().SetLicense("License.lic"); | |
// Create a workbook. | |
Workbook wb = new Workbook(); | |
// Optionally call this function if the workbook has no data | |
CreateSampleData(ref wb); | |
// Obtain the first sheet | |
Worksheet sheet = wb.Worksheets[0]; | |
// Add a new list object with 20 rows and 5 columns | |
ListObject listObject = sheet.ListObjects[sheet.ListObjects.Add("A1", "E20", true)]; | |
// Set table style | |
listObject.TableStyleType = TableStyleType.TableStyleMedium10; | |
// Show the flag to display the Total for all numbers | |
listObject.ShowTotals = true; | |
// Set the second column calculation type | |
listObject.ListColumns[1].TotalsCalculation = TotalsCalculation.Count; | |
// Saving the Excel file | |
wb.Save("output.xlsx"); | |
Console.WriteLine("Table created successfully"); | |
} | |
static void CreateSampleData(ref Workbook wb) | |
{ | |
// Fill workbook with some dummy data | |
string[] titles = new string[] {"Employee", "Quarter", "Product", "Country","Sale"}; | |
string[] employees = new string[] {"David", "James","Miya" }; | |
string[] products = new string[] { "Chai", "Chang", "Geitost", "Maxilaku" }; | |
string[] countries = new string[] { "Brazil", "China", "France", "Germany", "India", "Italy" }; | |
foreach (var (item, idx) in titles.Select((value, index) => (value, index))) | |
wb.Worksheets[0].Cells[0, idx].Value = item; | |
Random random = new Random(); | |
for(int i = 1; i < 20; i++) | |
{ | |
wb.Worksheets[0].Cells[i, 0].Value = employees[random.Next() % employees.Count()]; | |
wb.Worksheets[0].Cells[i, 1].Value = (random.Next() % 4) + 1; | |
wb.Worksheets[0].Cells[i, 2].Value = products[random.Next() % products.Count()]; | |
wb.Worksheets[0].Cells[i, 3].Value = countries[random.Next() % countries.Count()]; | |
wb.Worksheets[0].Cells[i, 4].Value = random.Next() % 2000; | |
} | |
} | |
} |
이 샘플 코드는 C#*에서 *MS Excel 테이블을 생성하는 프로세스를 보여줍니다. Worksheet 클래스에는 테이블을 추가하기 위한 ListObjects 개체가 포함되어 있습니다. TableStyleType에는 테이블을 생성하는 동안 사용할 수 있는 MS Excel에서 지원되는 모든 기본 테이블 스타일이 있습니다.
이 문서에서는 C#에서 고급 Excel 테이블을 사용하여 작업하는 방법을 설명했습니다. Excel 파일에 슬라이서를 삽입하려면 C#을 사용하여 Excel에 슬라이서를 삽입하는 방법의 문서를 참조하세요.