Định dạng ô Exceljs

Đó là cách tiếp cận được đề xuất và tối ưu hóa để định dạng toàn bộ hàng hoặc cột với cùng kiểu thay vì định dạng từng ô riêng lẻ. Sử dụng đoạn mã sau để đặt kiểu mặc định

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}

Một ví dụ hoạt động hoàn chỉnh để áp dụng kiểu mặc định cho các hàng và cột trong C# hiện có trên trang GitHub này

GHI CHÚ

Áp dụng kiểu tùy chỉnh sẽ ghi đè kiểu gốc

LỜI KHUYÊN

Để áp dụng kiểu cho toàn bộ cột thay vì áp dụng cho từng ô, hãy sử dụng kiểu mặc định

Áp dụng phong cách toàn cầu

XlsIO thêm các kiểu trên toàn cầu có thể được áp dụng cho một hoặc nhiều ô trong sổ làm việc. Đây là phương pháp được đề xuất để áp dụng một kiểu trong các hàng và cột khác nhau, giúp cải thiện đáng kể bộ nhớ và hiệu suất

Để tìm hiểu thêm về hiệu suất, hãy tham khảo phần Cải thiện hiệu suất để có hiệu suất tốt hơn trong XlsIO

Đoạn mã sau minh họa cách đặt kiểu tiêu đề và kiểu nội dung cho các ô

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
0

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
1

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
2

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
3

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
4

Một ví dụ hoạt động hoàn chỉnh để áp dụng kiểu toàn cầu trong C# có trên trang GitHub này

Định dạng ô Exceljs

Tài liệu Excel với Kiểu toàn cầu

Áp dụng định dạng số

Định dạng số là các mã giúp kiểm soát sự xuất hiện của các giá trị ô, đặc biệt là các số trong tài liệu Excel. Excel nhận dạng các số ở các định dạng khác nhau như

  • Số
  • Tiền tệ
  • tỷ lệ phần trăm
  • Ngày giờ
  • Kế toán
  • Có tính khoa học
  • phân số và
  • Chữ

Định dạng số này có thể có tối đa 4 phần, được phân tách bằng dấu chấm phẩy. họ đang

  • số dương
  • số âm
  • số không
  • Chữ

Mỗi phần là một định dạng số riêng lẻ. Định dạng mặc định là “General”, nó có nghĩa là bất cứ thứ gì phù hợp

Bảng sau đây hiển thị các mã định dạng tùy chỉnh khác nhau

Số Mã

Sự miêu tả

Chung

Định dạng số chung

0 (không)

Giữ chỗ chữ số. Mã này đệm giá trị bằng số không để điền vào định dạng

#

giữ chỗ chữ số. Mã này không hiển thị thêm số không

?

Giữ chỗ chữ số. Mã này chừa một khoảng trống cho các số 0 không đáng kể nhưng không hiển thị chúng

(giai đoạn = Stage)

giữ chỗ thập phân. Giữ chỗ thập phân xác định có bao nhiêu chữ số được hiển thị ở bên trái và bên phải của dấu tách thập phân

%

Giữ chỗ phần trăm. Nhân với 100 và thêm ký tự %

, (dấu phẩy)

Dấu phân cách hàng nghìn. Dấu phẩy theo sau bởi một trình giữ chỗ (0 hoặc #) tỷ lệ số theo một nghìn

E+ E- e+ e-

Ký hiệu khoa học

Mã văn bản

Sự miêu tả

$ - + / ( ). không gian

Các ký tự này được hiển thị trong số. Để hiển thị bất kỳ ký tự nào khác, hãy đặt ký tự đó trong dấu ngoặc kép hoặc đặt trước ký tự đó bằng dấu gạch chéo ngược

\nhân vật

Mã này hiển thị ký tự tiếp theo mà bạn chỉ định

Đánh máy ghi chú. , ^, &, ', ~, {, }, =, < hoặc > tự động đặt dấu gạch chéo ngược trước ký tự

"chữ"

Mã này hiển thị văn bản

*

Mã này lặp lại ký tự tiếp theo trong định dạng để lấp đầy chiều rộng của cột

Ghi chú. Chỉ cho phép một dấu hoa thị trên mỗi phần của định dạng

_ (gạch dưới)

Mã này bỏ qua chiều rộng của ký tự tiếp theo. Mã này thường được sử dụng là "_)" (không có dấu ngoặc kép) để chừa khoảng trống cho dấu ngoặc đơn đóng ở định dạng số dương khi định dạng số âm bao gồm dấu ngoặc đơn

Điều này cho phép các giá trị xếp hàng ở dấu thập phân

@

trình giữ chỗ văn bản

Mã ngày

Sự miêu tả

m

Tháng ở dạng số không có số 0 đứng đầu (1-12)

mm

Tháng là số có số 0 đứng đầu (01-12)

mmm

Tháng dưới dạng viết tắt (tháng 1 - tháng 12)

mmmm

Tháng không viết tắt (Tháng 1 - Tháng 12)

d

Ngày không có số 0 đứng đầu (1-31)

đ

Ngày có số 0 đứng đầu (31-01)

đđ

Ngày trong tuần viết tắt (Sun - Sat)

đdd

Ngày trong tuần không viết tắt (Chủ Nhật - Thứ Bảy)

yy

Năm ở dạng số có hai chữ số (ví dụ: 96)

yyyy

Năm dưới dạng số có bốn chữ số (ví dụ: 1996)

mã thời gian

Sự miêu tả

h

Giờ dưới dạng số không có số 0 đứng đầu (0-23)

hh

Giờ dưới dạng số có số 0 đứng đầu (00-23)

m

Phút dưới dạng số không có số 0 đứng đầu (0-59)

mm

Phút dưới dạng số có số 0 đứng đầu (00-59)

s

Giây dưới dạng số không có số 0 đứng đầu (0-59)

ss

Giây dưới dạng số có số 0 đứng đầu (00-59)

Sáng/Chiều Sáng/Chiều

Thời gian dựa trên đồng hồ mười hai giờ

Mã linh tinh

Sự miêu tả

[BLACK], [BLUE], [CYAN], [GREEN], [MAGENTA], [RED], [TRẮNG], [VÀNG], [COLOR n]

Các mã này hiển thị các ký tự trong các màu được chỉ định

Ghi chú. n là giá trị từ 1 đến 56 và đề cập đến màu thứ n trong bảng màu

[Giá trị điều kiện]

Condition may be <, >, =, >=, <=, <> and value may be any number.

Ghi chú. Một định dạng số có thể chứa tối đa hai điều kiện

XlsIO cung cấp hỗ trợ đọc và ghi nhiều định dạng số tùy chỉnh và tích hợp sẵn khác nhau trong một ô bằng cách sử dụng thuộc tính của giao diện IRange

Đoạn mã sau minh họa cách đặt các định dạng số khác nhau trong một phạm vi trang tính

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
5

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
6

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
7

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
8

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
9

Ảnh chụp màn hình của mã trước đó được hiển thị như sau

Một ví dụ hoạt động hoàn chỉnh để áp dụng các định dạng số trong C# hiện có trên trang GitHub này

Định dạng ô Exceljs

Kết quả áp dụng định dạng số truy cập khi chạy

Các giá trị ô có thể được truy cập dưới dạng , và của giao diện IRange. Ngoài ra, còn có một thuộc tính khác trong IRange, thuộc tính này trả về giá trị kết quả của một ô có định dạng số được áp dụng

Ví dụ mã sau đây minh họa cách hiển thị văn bản của một ô

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.FromArgb(255, 144, 238, 144);
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Style";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.FromArgb(255, 144, 238, 144);
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Style";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
1

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.FromArgb(255, 144, 238, 144);
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Style";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
2

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.FromArgb(255, 144, 238, 144);
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Style";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
3

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.FromArgb(255, 144, 238, 144);
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Style";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
4

Bạn có thể đặt thuộc tính của IWorkbook là false với thuộc tính, nếu bạn chắc chắn rằng giá trị đã cho không thuộc kiểu dữ liệu DateTime giúp cải thiện hiệu suất thời gian

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.FromArgb(255, 144, 238, 144);
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Style";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.FromArgb(255, 144, 238, 144);
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Style";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
6

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.FromArgb(255, 144, 238, 144);
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Style";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.FromArgb(255, 144, 238, 144);
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Style";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.FromArgb(255, 144, 238, 144);
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Style";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
5

Ẩn nội dung ô bằng cách đặt Định dạng số

Essential XlsIO hỗ trợ trong một trang tính cùng với. Bạn cũng có thể ẩn một nội dung ô cụ thể bằng cách đặt định dạng số cụ thể cho ô đó

Tham khảo các đoạn mã hoàn chỉnh sau đây

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  FileStream stream = new FileStream("Style.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  FileStream stream = new FileStream("Style.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
1

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  FileStream stream = new FileStream("Style.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
2

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  FileStream stream = new FileStream("Style.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
3

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  FileStream stream = new FileStream("Style.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
4

Một ví dụ hoạt động hoàn chỉnh để ẩn nội dung ô bằng cách áp dụng định dạng số trong C# có trên trang GitHub này

Áp dụng căn chỉnh văn bản ô

XlsIO hỗ trợ các tùy chọn căn chỉnh sau

  • Căn ngang
  • Căn dọc
  • thụt đầu dòng
  • Định hướng
  • Văn bản chỉ đạo

Căn ngang

Đoạn mã này căn chỉnh nội dung ô theo chiều ngang

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  FileStream stream = new FileStream("Style.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  FileStream stream = new FileStream("Style.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
6

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  FileStream stream = new FileStream("Style.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  FileStream stream = new FileStream("Style.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  FileStream stream = new FileStream("Style.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
5

Căn dọc

Đoạn mã này căn chỉnh nội dung ô theo chiều dọc

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Syncfusion.Drawing.Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Syncfusion.Drawing.Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
}
1

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Syncfusion.Drawing.Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Syncfusion.Drawing.Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Syncfusion.Drawing.Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
}
0

thụt đầu dòng

Điều này cho phép đặt nội dung ô để di chuyển nội dung đó đến gần đường viền ô hoặc di chuyển nội dung ra xa đường viền ô

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Syncfusion.Drawing.Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Syncfusion.Drawing.Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
}
6

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Syncfusion.Drawing.Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Syncfusion.Drawing.Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Creating a new style with cell back color, fill pattern and font attribute
  IStyle style = workbook.Styles.Add("NewStyle");
  style.Color = Syncfusion.Drawing.Color.LightGreen;
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal;
  style.Font.Bold = true;
  worksheet.Range["B2"].CellStyle = style;

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Style.xlsx", "application/msexcel", stream);
  }
}
5

Định hướng

Thao tác này giúp xoay văn bản trong ô theo đường chéo hoặc chiều dọc. Hướng văn bản có thể được đặt bằng cách sử dụng thuộc tính như được hiển thị như sau

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}
1

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}
0

Văn bản chỉ đạo

Bạn có thể chỉ định hướng văn bản bằng cách sử dụng thuộc tính như được hiển thị như sau

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}
6

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  workbook.SaveAs("DefaultStyles.xlsx");
}
5

Sau đây là đoạn mã hoàn chỉnh minh họa các tùy chọn trước đó

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using
0

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using
1

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using
2

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using
3

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using
4

Một ví dụ hoạt động hoàn chỉnh để áp dụng căn chỉnh văn bản ô trong C# có trên trang GitHub này

Ảnh chụp màn hình sau đây là đầu ra của mã trước đó

Định dạng ô Exceljs

Hợp nhất và hủy hợp nhất các ô

Các ô có thể được hợp nhất bằng phương pháp trong IRange như được hiển thị như sau

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using
5

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using
6

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using
5

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using
5

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Define new styles to apply in rows and columns
  Dim rowStyle As IStyle = workbook.Styles.Add("RowStyle")
  rowStyle.Color = Color.LightGreen
  Dim columnStyle As IStyle = workbook.Styles.Add("ColumnStyle")
  columnStyle.Color = Color.Orange

  'Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle)
  'Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle)

  workbook.SaveAs("DefaultStyles.xlsx")
End Using
5

Các ô đã hợp nhất có thể được hủy hợp nhất bằng cách sử dụng phương thức trong IRange như hình bên dưới

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
1

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
0

Đoạn mã dưới đây hiển thị các ô trang tính hợp nhất và hủy hợp nhất

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
6

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
7

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
8

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.FromArgb(255, 144, 238, 144);
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.FromArgb(255, 255, 165, 0);

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "DefaultStyles";
  savePicker.FileTypeChoices.Add("Excel Files", new List<string>() { ".xlsx" });

  //Creates a storage file from FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Saves changes to the specified storage file
  await workbook.SaveAsAsync(storageFile);
}
9

Một ví dụ hoạt động hoàn chỉnh để hợp nhất và hủy hợp nhất các ô trong C# có trên trang GitHub này

Áp dụng văn bản ngắt

Nếu nội dung ô quá rộng để vừa với cột và không muốn chia thành các ô liền kề, bạn có thể sử dụng thuộc tính. Điều này sẽ đặt nội dung trong viền ô. Đoạn mã sau minh họa hành vi này

GHI CHÚ

Áp dụng văn bản bao bọc sẽ không tự động điều chỉnh các hàng theo mặc định. Bạn nên [tự động điều chỉnh](#_AutoFit_Rows_or “”) theo cách thủ công

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
1

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
2

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
3

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
4

Một ví dụ hoạt động hoàn chỉnh để áp dụng văn bản ngắt dòng trong C# hiện có trên trang GitHub này

Mở văn bản

Tương tự, văn bản được bọc có thể được mở bằng cách vô hiệu hóa thuộc tính. Đoạn mã sau giải thích điều này

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
6

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  FileStream stream = new FileStream("DefaultStyles.xlsx", FileMode.Create, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  stream.Dispose();
}
5

Tự động khớp hàng hoặc cột

Kích thước ô có thể được tự động định cỡ theo nội dung của nó để hiển thị nội dung của nó

Đoạn mã sau cho biết cách tự động định cỡ chiều cao hàng và chiều rộng cột cho nội dung ô của nó

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}
0

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}
1

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}
2

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}
3

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}
4

Một ví dụ hoạt động hoàn chỉnh để tự động điều chỉnh hàng và cột trong C# có trên trang GitHub này

Áp dụng cài đặt phông chữ

Hình thức của văn bản có thể được kiểm soát bằng cài đặt phông chữ của ô. Các cài đặt này có thể được thực hiện bằng cách sử dụng thuộc tính trong CellStyle. Tham khảo đoạn mã sau

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}
5

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}
6

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}
7

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}
8

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Create(1);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Define new styles to apply in rows and columns
  IStyle rowStyle = workbook.Styles.Add("RowStyle");
  rowStyle.Color = Syncfusion.Drawing.Color.LightGreen;
  IStyle columnStyle = workbook.Styles.Add("ColumnStyle");
  columnStyle.Color = Syncfusion.Drawing.Color.Orange;

  //Set default row style for entire row
  worksheet.SetDefaultRowStyle(1, 2, rowStyle);
  //Set default column style for entire column
  worksheet.SetDefaultColumnStyle(1, 2, columnStyle);

  //Saving the workbook as stream
  MemoryStream stream = new MemoryStream();
  workbook.SaveAs(stream);

  stream.Position = 0;

  //Save the document as file and view the saved document
  //The operation in SaveAndView under Xamarin varies between Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
  {
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
  else
  {
    Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("DefaultStyles.xlsx", "application/msexcel", stream);
  }
}
9

Một ví dụ hoạt động hoàn chỉnh để áp dụng các cài đặt phông chữ khác nhau trong C# có trên trang GitHub này

Đầu ra của mã trước đó được hiển thị như sau

Định dạng ô Exceljs

Áp dụng cài đặt màu

Màu sắc giúp nâng cao giá trị ô để làm nổi bật dữ liệu. Các cài đặt màu này trong một ô được phân biệt là BackColor, ForeColor và PatternColor

Cài đặt màu nền

Màu nền của một ô có thể được đặt bằng thuộc tính của CellStyle như được hiển thị như sau

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
00

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
01

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
00

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
00

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
00

Cài đặt màu trước

Màu trước của một ô có thể được đặt bằng thuộc tính của CellStyle như được hiển thị như sau

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
05

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
06

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
05

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
05

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
05

Cài đặt mẫu

Excel cung cấp các kiểu mẫu khác nhau để tô sáng các ô. Các mẫu này có thể được áp dụng bằng cách sử dụng thuộc tính của CellStyle như sau

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
10

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
11

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
10

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
10

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
10

Một ví dụ hoạt động hoàn chỉnh để áp dụng các cài đặt màu khác nhau trong C# có trên trang GitHub này

Áp dụng cài đặt đường viền

XlsIO áp dụng đường viền ô và định dạng nó thông qua giao diện IBorderApplying HTML String như sau

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
15

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
16

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
17

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
18

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
19

Một ví dụ hoạt động hoàn chỉnh để áp dụng các cài đặt đường viền khác nhau trong C# có trên trang GitHub này

Đầu ra của mã trước đó được hiển thị trong ảnh chụp màn hình sau

Định dạng ô Exceljs

Định dạng chuỗi HTML

Chuỗi HTML tạo ra một chuỗi ký tự với các kiểu định dạng khác nhau bằng cách sử dụng các thẻ HTML khác nhau cho mỗi ký tự. Điều này giúp dễ dàng thao tác với văn bản hoặc giá trị trong phạm vi trang tính vì mỗi ký tự độc lập và không phụ thuộc vào cấu trúc thẻ phân cấp

XlsIO hỗ trợ thêm HTML Rich-Text vào một dải ô trong trang tính

Áp dụng chuỗi HTML

Đoạn mã sau minh họa cách đọc và viết HTML Rich-Text sử dụng thuộc tính của IRange

  • C#
  • VB. NET
  • UWP
  • ASP. NET lõi
  • Xamarin

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
20

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
21

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
22

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
22

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Creating a new style with cell back color, fill pattern and font attribute
  Dim style As IStyle = workbook.Styles.Add("NewStyle")
  style.Color = Color.LightGreen
  style.FillPattern = ExcelPattern.DarkUpwardDiagonal
  style.Font.Bold = True
  worksheet.Range("B2").CellStyle = style

  workbook.SaveAs("Style.xlsx")
End Using
22

Thẻ được hỗ trợ

Sau đây là danh sách các thẻ được hỗ trợ ngoài chuỗi HTML

  • - Defines a paragraph
  • - Defines font, color and size of text
  • - Xác định tiêu đề HTML
  • - Defines a hyperlink
  • - Defines italic text
  • - Underlines the text
  • - Defines bold text
  • - Defines subscript
  • - Defines superscript


  • - Inserts link break
  • - Strikes out the text
  • - Defines important text

Các thẻ được đề cập này hỗ trợ các thuộc tính kiểu sau

  • Màu
  • họ phông chữ
  • trang trí văn bản
  • Cỡ chữ
  • Kích thước
  • Đối mặt

Định dạng Rich-Text

Bạn có thể định dạng từng ký tự trong một ô với các kiểu phông chữ khác nhau. XlsIO đọc và ghi văn bản có định dạng bằng cách sử dụng giao diện IRichTextString

ExcelJS có hỗ trợ XLS không?

exceljs không hỗ trợ xls , đây là định dạng độc quyền, cũ hơn. xlsx là một tiêu chuẩn mở và chỉ nén xml, rất dễ hỗ trợ.

ExcelJS là gì?

ExcelJS là gói JavaScript hữu ích hoạt động như một trình quản lý sổ làm việc Excel . Hướng dẫn này sẽ giúp bạn đọc, thao tác và ghi dữ liệu bảng tính vào XLSX và JSON. Viết bởiWai Foong Ng. Xuất bản vào tháng 12. 21, 2022.

Làm cách nào để ghi dữ liệu trong trang tính Excel bằng JavaScript?

Cách nhập và xuất Excel XLSX bằng JavaScript .
Thiết lập Dự án Bảng tính JavaScript
Thêm mã nhập Excel
Thêm dữ liệu vào tệp Excel đã nhập
Thêm một biểu đồ thu nhỏ
Thêm mã xuất Excel

Làm cách nào để sử dụng ExcelJS trong Góc?

Làm cách nào để tải xuống XLSX từ JSON bằng ExcelJS trên ứng dụng góc? .
Bước 1 - Tạo ứng dụng góc
Bước 2 – Cài đặt ExcelJS và File-Saver
Bước 3 – Định cấu hình tsconfig. json
Bước 4 – Tạo & Cập nhật Excel Export Service
Bước 5 – Tạo và lưu Trình tiết kiệm tệp XLSX
Bước 6 – Xem trong hành động