OpenXml sdk 2.0으로 Excel 문서 작성
OpenXml SDK 2.0을 사용하여 Excel 문서를 만들었는데, 이제 스타일을 지정해야 하는데 할 수가 없습니다.
배경색을 칠하거나 다른 셀에서 글꼴 크기를 변경하는 방법을 모르겠습니다.
셀을 만드는 내 코드는 다음과 같습니다.
private static Cell CreateTextCell(string header, string text, UInt32Value index)
{
Cell c = new Cell();
c.DataType = CellValues.InlineString;
c.CellReference = header + index;
InlineString inlineString = new InlineString();
DocumentFormat.OpenXml.Spreadsheet.Text t = new DocumentFormat.OpenXml.Spreadsheet.Text();
t.Text = text;
inlineString.AppendChild(t);
c.AppendChild(inlineString);
return c;
}
참고: OpenXML 2.0 SDK는 현재 CTP에 포함되어 있으며 Office 2010 이전에는 프로덕션용으로 라이센스가 부여되지 않았습니다.
OpenXML SDK를 다루는 일반적인 방법은 구현 방법(배경색 등)을 배우고자 하는 기능만 있는 빈 문서와 문서를 만들고 SDK의 OpenXmlDiff를 사용하여 이 기능을 구현하기 위해 어떤 변경이 필요한지 확인하는 것입니다.
처음부터 문서를 작성하는 경우, Document Reflector를 사용하여 기본 스타일시트 오브젝트에 대한 코드를 생성한 다음 필요한 스타일을 추가할 수 있습니다.
기본값으로 시작:
new Stylesheet(
new Fonts(
new Font(
new FontSize() { Val = 10D },
new Color() { Theme = (UInt32Value)1U },
new FontName() { Val = "Arial" },
new FontFamilyNumbering() { Val = 2 })
) { Count = (UInt32Value)1U },
new Fills(
new Fill(
new PatternFill() { PatternType = PatternValues.None }),
new Fill(
new PatternFill() { PatternType = PatternValues.Gray125 })
) { Count = (UInt32Value)2U },
new Borders(...
...
...
new CellFormats(
new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }) { Count = (UInt32Value)1U }, ...
크기가 12인 새 글꼴과 빨간색 배경으로 채우기(색인 값 64)를 추가했고, 새 글꼴 및 채우기의 색인을 참조하는 새 셀 형식을 추가했습니다. (카운트도 업데이트하십시오.)
new Stylesheet(
new Fonts(
new Font(
new FontSize() { Val = 10D },
new Color() { Theme = (UInt32Value)1U },
new FontName() { Val = "Arial" },
new FontFamilyNumbering() { Val = 2 }),
new Font(
new FontSize() { Val = 12D },
new Color() { Theme = (UInt32Value)1U },
new FontName() { Val = "Arial" },
new FontFamilyNumbering() { Val = 2 })
) { Count = (UInt32Value)2U },
new Fills(
new Fill(
new PatternFill() { PatternType = PatternValues.None }),
new Fill(
new PatternFill() { PatternType = PatternValues.Gray125 }),
new Fill(
new PatternFill() { PatternType = PatternValues.Solid, ForegroundColor = new ForegroundColor() { Rgb = "FFFF0000" }, BackgroundColor = new BackgroundColor() { Indexed = 64 } })
) { Count = (UInt32Value)3U },
new Borders(
new Border(
new LeftBorder(), new RightBorder(), new TopBorder(), new BottomBorder(), new DiagonalBorder())
) { Count = (UInt32Value)1U },
new CellStyleFormats(
new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U }
) { Count = (UInt32Value)1U },
new CellFormats(
new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U },
new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)1U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U },
new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)2U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }
) { Count = (UInt32Value)3U },
new CellStyles(
new CellStyle() { Name = "Normal", FormatId = (UInt32Value)0U, BuiltinId = (UInt32Value)0U }
) { Count = (UInt32Value)1U },
new DifferentialFormats() { Count = (UInt32Value)0U },
new TableStyles() { Count = (UInt32Value)0U, DefaultTableStyle = "TableStyleMedium9", DefaultPivotStyle = "PivotStyleLight16" });
그런 다음 코드에서 CellStyle 인덱스를 포맷할 셀에 적용합니다. (셀 A2와 A3에는 이미 데이터가 있습니다.)셀 A2는 더 큰 크기, A3는 빨간색 배경을 갖습니다.)
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
sheetData.Descendants<Row>().Where(r => r.RowIndex == 2U).First().Descendants<Cell>().First().StyleIndex = 1U;
sheetData.Descendants<Row>().Where(r => r.RowIndex == 3U).First().Descendants<Cell>().First().StyleIndex = 2U;
이 기사를 써주셔서 감사합니다.
많은 어려움을 겪은 후(그리고 구글링을 통해), 저는 마침내 매우 사용하기 쉬운 C# 클래스를 만들 수 있었습니다. C# 클래스는 DataSet과 Filename을 사용하고 DataSet의 데이터를 포함하는 Office 2007 .xlsx를 만듭니다.
갑자기 응용프로그램에 "Export to Excel" 기능을 추가하는 프로세스가 매우 쉬워집니다.
DataSet ds = CreateSampleData(); // Your code here !
string excelFilename = "C:\\Sample.xlsx";
CreateExcelFile.CreateExcelDocument(ds, excelFilename);
저는 다음 웹사이트에 전체 소스 코드와 사용 예시를 게시했습니다.
Visual Studio 2008 C# WinForms 응용 프로그램이지만 VS2010을 실행하는 경우 Visual Studio에서 이 프로젝트를 업그레이드할 수 있습니다.
즐거운 시간 되세요.
http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm
셀 유형을 지정하는 방법
new Cell() { CellReference = "B6", StyleIndex = 11U }
여기서 "11U"는 StylesPart의 0 기반 인덱스입니다.스타일시트.각 셀 형식이 숫자 조합을 정의하는 셀 형식형식, 글꼴, 채우기 및 테두리 스타일.
프로그램별로 모든 스타일을 추가할 필요는 없으며, 필요한 모든 형식이 포함된 템플릿 xlsx 파일을 만든 다음 프로그램에서 스타일 인덱스를 지정할 수 있습니다.
언급URL : https://stackoverflow.com/questions/1012547/creating-excel-document-with-openxml-sdk-2-0
'programing' 카테고리의 다른 글
| 첫 번째 문자 Vue 2에서 시작하는 요소 표시 (0) | 2023.06.14 |
|---|---|
| 내 단추 텍스트가 롤리팝의 모든 캡스에 강제로 적용되는 이유는 무엇입니까? (0) | 2023.06.14 |
| C# ASP.NET Single Sign-On 구현 (0) | 2023.06.14 |
| 소장품을 정리하는 이상적인 방법 (0) | 2023.06.14 |
| 컬렉션 항목의 값을 변경하는 방법 (0) | 2023.06.14 |