Node.js ile C++ aracılığıyla hücreleri biçimlendirin

Giriş

GetStyle ve SetStyle Metodları Kullanılarak Hücreleri Biçimlendirme

Hücrelere arka plan veya ön plan renkleri, kenarlıklar, fontlar, yatay ve dikey hizalamalar, girinti düzeyi, yazı yönü, döndürme açısı ve çok daha fazlası için farklı türde biçimlendirme stilleri uygulayın.

GetStyle ve SetStyle Metodlarını Kullanma

Eğer geliştiriciler farklı hücrelere farklı biçimlendirme stilleri uygulamak istiyorsa, hücrenin Style öğesini Cell.getStyle() metodu ile almak, stil özelliklerini belirlemek ve ardından biçimlendirmeyi Cell.setStyle(Style) metodu ile uygulamak daha iyidir. Aşağıda, bu yaklaşımı göstermek için bir örnek verilmiştir.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();

// Obtaining the reference of the first worksheet
const worksheet = workbook.getWorksheets().get(0);

// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");

// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");

// Defining a Style object
let style;

// Get the style from A1 cell
style = cell.getStyle();

// Setting the vertical alignment
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);

// Setting the horizontal alignment
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);

// Setting the font color of the text
style.getFont().setColor(AsposeCells.Color.Green);

// Setting to shrink according to the text contained in it
style.setShrinkToFit(true);

// Setting the bottom border color to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);

// Setting the bottom border type to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);

// Applying the style to A1 cell
cell.setStyle(style);

// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));

Farklı Hücreleri Biçimlendirmek İçin Stil Nesnesini Kullanma

Eğer geliştiriciler aynı biçimlendirme stilini farklı hücrelere uygulamak istiyorsa, Style nesnesini kullanabilirler. Style nesnesini kullanmak için aşağıdaki adımları takip edin:

  1. Workbook sınıfının createStyle() metodunu çağırarak bir Style nesnesi ekleyin
  2. Yeni eklenen Style nesnesine erişin
  3. İstenen biçimlendirme ayarlarını uygulamak için Style nesnesinin ilgili özelliklerini/atribütlerini ayarlayın
  4. Yapılandırılmış Style nesnesini istediğiniz hücrelere atayın

Bu yaklaşım uygulamalarınızın verimliliğini büyük ölçüde artırabilir ve aynı zamanda bellek tasarrufu sağlayabilir.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();

// Adding a new worksheet to the Excel object
const i = workbook.getWorksheets().add();

// Obtaining the reference of the first worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(i);

// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");

// Adding some value to the "A1" cell
cell.putValue("Hello Aspose!");

// Adding a new Style
const style = workbook.createStyle();

// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);

// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);

// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);

// Shrinking the text to fit in the cell
style.setShrinkToFit(true);

// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);

// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);

// Assigning the Style object to the "A1" cell
cell.setStyle(style);

// Apply the same style to some other cells
worksheet.getCells().get("B1").setStyle(style);
worksheet.getCells().get("C1").setStyle(style);
worksheet.getCells().get("D1").setStyle(style);

// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));

Microsoft Excel 2007 Önceden Tanımlanmış Stilleri Nasıl Kullanılır

Microsoft Excel 2007 için farklı biçimlendirme stilleri uygulamanız gerekiyorsa, Aspose.Cells API’sını kullanarak stilleri uygulayın. Aşağıdaki örnek, bir hücreye önceden tanımlanmış bir stil uygulamanın bu yaklaşımını gösterir.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");


// Instantiate a new Workbook.
const workbook = new AsposeCells.Workbook();

// Create a style object.
const style = workbook.createStyle();

// Input a value to A1 cell.
workbook.getWorksheets().get(0).getCells().get("A1").putValue("Test");

// Apply the style to the cell.
workbook.getWorksheets().get(0).getCells().get("A1").setStyle(style);

// Save the Excel 2007 file.
workbook.save(path.join(dataDir, "book1.out.xlsx"));

Bir Hücredeki Seçili Karakterleri Biçimlendirme

Yazı tipi ayarlarıyla ilgilenmek, hücre içindeki metni biçimlendirmeyi açıklar, ancak sadece hücre içeriğinin tümünü biçimlendirmeyi açıklar. Seçili karakterleri biçimlendirmek istiyorsanız ne yapacaksınız?

Aspose.Cells bu özelliği de destekler. Bu konu, bu özelliğin etkin kullanımını açıklar.

Seçili Karakterleri Biçimlendirmek

Aspose.Cells, Microsoft Excel dosyasını temsil eden Workbook sınıfını sağlar. Workbook sınıfı, Excel dosyasındaki her çalışma sayfasına erişimi sağlayan getWorksheets() koleksiyonunu içerir. Bir çalışma sayfası, Worksheet sınıfı ile temsil edilir. Worksheet sınıfı, getCells() koleksiyonunu sağlar. getCells() koleksiyonundaki her öğe, Cell sınıfı nesnesini temsil eder.

Cell sınıfı, aşağıdaki parametreleri alan characters(number, number) metodunu sağlar; böylece bir hücre içindeki karakter aralığını seçebilirsiniz:

  • Başlangıç İndeksi, seçimin nereden başlayacağı karakterin indeksi.
  • Karakter Sayısı, seçilecek karakterlerin sayısı.

characters(number, number) metodu, geliştiricilerin aşağıdaki kod örneğinde gösterildiği gibi hücreyle aynı şekilde karakterleri biçimlendirmelerine izin veren FontSetting sınıfı örneği döndürür. Çıktı dosyasında, A1 hücresinde ‘Visit’ kelimesi varsayılan font ile biçimlendirilirken, ‘Aspose!’ kalın ve mavi olacaktır.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();

// Obtaining the reference of the first(default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);

// Accessing the "A1" cell from the worksheet
const cell = worksheet.getCells().get("A1");

// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");

// Setting the font of selected characters to bold
const font = cell.characters(6, 7).getFont();
font.isBold = true;

// Setting the font color of selected characters to blue
font.color = AsposeCells.Color.Blue;

// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));

Satırları ve Sütunları Nasıl Biçimlendirilir

Bazı durumlarda, geliştiricilerin satırlara veya sütunlara aynı biçimlendirmeyi uygulamaları gerekebilir. Hücrelere tek tek biçimlendirme uygulamak genellikle daha uzun sürer ve iyi bir çözüm değildir. Bu sorunu ele almak için, Aspose.Cells bu makalede detaylı bir şekilde tartışılan basit ve hızlı bir yol sağlar.

Satırları ve Sütunları Biçimlendirme

Aspose.Cells, Microsoft Excel dosyasını temsil eden Workbook sınıfını sağlar. Workbook sınıfı, Excel dosyasındaki her çalışma sayfasına erişimi sağlayan getWorksheets() koleksiyonunu içerir. Bir çalışma sayfası, Worksheet sınıfı ile temsil edilir. Worksheet sınıfı, getCells() koleksiyonunu sağlar. getCells() koleksiyonunda, bir getRows() nesnesini temsil eden öğeler bulunur.

Bir Satırı Nasıl Biçimlendirirsiniz

getRows() koleksiyonundaki her öğe, bir Row nesnesini temsil eder. Row nesnesi, satırın biçimlendirmesini ayarlamak için kullanılan applyStyle(Style, StyleFlag) metodunu sunar. Aynı biçimlendirmeyi bir satıra uygulamak için, Style nesnesini kullanın. Aşağıdaki adımlar, nasıl kullanılacağını gösterir.

  1. createStyle() metodunu çağırarak Workbook sınıfına Style nesnesi ekleyin.
  2. Style nesnesinin özelliklerini ayarlayarak biçimlendirme ayarları uygulayın.
  3. StyleFlag nesnesinin ilgili özelliklerini YASAKLAYIN.
  4. Yapılandırılmış Style nesnesini Row nesnesine atayın.
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();

// Obtaining the reference of the first (default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);

// Adding a new Style to the styles
const style = workbook.createStyle();

// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);

// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);

// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);

// Shrinking the text to fit in the cell
style.setShrinkToFit(true);

// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);

// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);

// Creating StyleFlag
const styleFlag = new AsposeCells.StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBorders(true);
styleFlag.setFontColor(true);

// Accessing a row from the Rows collection
const row = worksheet.getCells().getRows().get(0);

// Assigning the Style object to the Style property of the row
row.applyStyle(style, styleFlag);

// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));

Bir Sutunu Nasıl Biçimlendirirsiniz

getCells() koleksiyonu ayrıca bir getColumns() koleksiyonu da sağlar. getColumns() koleksiyonundaki her öğe bir Column nesnesini temsil eder. Bir Row nesnesine benzer şekilde, Column nesnesi de sütunu biçimlendirmek için applyStyle(Style, StyleFlag) metodunu sunar.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();

// Obtaining the reference of the first (default) worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(0);

// Adding a new Style to the styles
const style = workbook.createStyle();

// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(AsposeCells.TextAlignmentType.Center);

// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(AsposeCells.TextAlignmentType.Center);

// Setting the font color of the text in the "A1" cell
style.getFont().setColor(AsposeCells.Color.Green);

// Shrinking the text to fit in the cell
style.setShrinkToFit(true);

// Setting the bottom border color of the cell to red
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(AsposeCells.Color.Red);

// Setting the bottom border type of the cell to medium
style.getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Medium);

// Creating StyleFlag
const styleFlag = new AsposeCells.StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBorders(true);
styleFlag.setFontColor(true);

// Accessing a column from the Columns collection
const column = worksheet.getCells().getColumns().get(0);

// Applying the style to the column
column.applyStyle(style, styleFlag);

// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));

Gelişmiş Konular