Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Basic Table

Jan Havlíček edited this pageJun 6, 2024 ·4 revisions

Important

This page is obsolete and no longer being actively maintained. Please refer to pageTables atdocs.closedxml.io for the latest information.

BasicTable.jpg

Creating a new workbook

varwb=newXLWorkbook();

Adding a worksheet

varws=wb.Worksheets.Add("Contacts");

Adding text

// Titlews.Cell("B2").Value="Contacts";// First Namesws.Cell("B3").Value="FName";ws.Cell("B4").Value="John";ws.Cell("B5").Value="Hank";ws.Cell("B6").Value="Dagny";// Last Namesws.Cell("C3").Value="LName";ws.Cell("C4").Value="Galt";ws.Cell("C5").Value="Rearden";ws.Cell("C6").Value="Taggart";

Adding more data types

// Booleanws.Cell("D3").Value="Outcast";ws.Cell("D4").Value=true;ws.Cell("D5").Value=false;ws.Cell("D6").Value=false;// DateTimews.Cell("E3").Value="DOB";ws.Cell("E4").Value=newDateTime(1919,1,21);ws.Cell("E5").Value=newDateTime(1907,3,4);ws.Cell("E6").Value=newDateTime(1921,12,15);// Numericws.Cell("F3").Value="Income";ws.Cell("F4").Value=2000;ws.Cell("F5").Value=40000;ws.Cell("F6").Value=10000;

Defining ranges

// From worksheetvarrngTable=ws.Range("B2:F6");// From another rangevarrngDates=rngTable.Range("D3:D5");// The address is relative to rngTable (NOT the worksheet)varrngNumbers=rngTable.Range("E3:E5");// The address is relative to rngTable (NOT the worksheet)

Formatting dates and numbers

// Using OpenXML's predefined formatsrngDates.Style.NumberFormat.NumberFormatId=15;// Using a custom formatrngNumbers.Style.NumberFormat.Format="$ #,##0";

Formatting headers

varrngHeaders=rngTable.Range("A2:E2");// The address is relative to rngTable (NOT the worksheet)rngHeaders.Style.Alignment.Horizontal=XLAlignmentHorizontalValues.Center;rngHeaders.Style.Font.Bold=true;rngHeaders.Style.Fill.BackgroundColor=XLColor.Aqua;

Adding grid lines

rngTable.Style.Border.BottomBorder=XLBorderStyleValues.Thin;

Format title cell

rngTable.Cell(1,1).Style.Font.Bold=true;rngTable.Cell(1,1).Style.Fill.BackgroundColor=XLColor.CornflowerBlue;rngTable.Cell(1,1).Style.Alignment.Horizontal=XLAlignmentHorizontalValues.Center;

Merge title cells

rngTable.Row(1).Merge();// We could've also used: rngTable.Range("A1:E1").Merge()

Add thick borders

//Add a thick outside borderrngTable.Style.Border.OutsideBorder=XLBorderStyleValues.Thick;// You can also specify the border for each side with:// rngTable.FirstColumn().Style.Border.LeftBorder = XLBorderStyleValues.Thick;// rngTable.LastColumn().Style.Border.RightBorder = XLBorderStyleValues.Thick;// rngTable.FirstRow().Style.Border.TopBorder = XLBorderStyleValues.Thick;// rngTable.LastRow().Style.Border.BottomBorder = XLBorderStyleValues.Thick;

Adjust column widths to their content

ws.Columns(2,6).AdjustToContents();

Saving the workbook

wb.SaveAs("BasicTable.xlsx");

FAQ

Examples

Real world scenarios

Time Savers

Performance and Memory

Misc

Inserting Data/Tables

Styles

Ranges

Rows

Columns

Page Setup (Print Options)

AutoFilters

Comments

Dev docs

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp