page 156

Align Text in PowerPoint Table in Java

2019-03-26 04:04:27 Written by Koohji

This article demonstrates how to align text within a table cell in PowerPoint using Spire.Presenatation for Java.

Code Snippets

import com.spire.presentation.*;

public class AlignTextInTableCell {

    public static void main(String[] args) throws Exception {

        //create a PowerPoint file
        Presentation presentation = new Presentation();

        //add a table
        Double[] widths = new Double[]{100d, 200d, 100d, 100d};
        Double[] heights = new Double[]{30d, 70d, 70d};
        ITable table = presentation.getSlides().get(0).getShapes().appendTable(30,80, widths, heights);
        table.setStylePreset(TableStylePreset.NONE);

        //set the horizontal alignment for the cells in the first row
        table.get(0, 0).getTextFrame().setText("Left");
        table.get(0, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.LEFT);
        table.get(1, 0).getTextFrame().setText("Horizontal Center");
        table.get(1, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
        table.get(2, 0).getTextFrame().setText("Right");
        table.get(2, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
        table.get(3, 0).getTextFrame().setText("Justify");
        table.get(3, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.JUSTIFY);

        //Set the vertical alignment for the cells in the second row
        table.get(0, 1).getTextFrame().setText("Top");
        table.get(0, 1).setTextAnchorType(TextAnchorType.TOP);
        table.get(1, 1).getTextFrame().setText("Vertical Center");
        table.get(1, 1).setTextAnchorType(TextAnchorType.CENTER);
        table.get(2, 1).getTextFrame().setText("Bottom");
        table.get(2, 1).setTextAnchorType(TextAnchorType.BOTTOM);

        //set the both horizontal and vertical alignment for the cells in the third row
        table.get(0, 2).getTextFrame().setText("Top Left");
        table.get(0, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.LEFT);
        table.get(0, 2).setTextAnchorType(TextAnchorType.TOP);
        table.get(1, 2).getTextFrame().setText("Center");
        table.get(1, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
        table.get(1, 2).setTextAnchorType(TextAnchorType.CENTER);
        table.get(2, 2).getTextFrame().setText("Bottom Right");
        table.get(2, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
        table.get(2, 2).setTextAnchorType(TextAnchorType.BOTTOM);

        //save to file
        presentation.saveToFile("output/Alignment.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Output

Align Text in PowerPoint Table in Java

With Spire.XLS, developers can add text or image to the textbox to Excel worksheet easily. From version 9.3.10, Spire.XLS supports to set the inner margin of contents on Excel text box. With this feature, we can adjust the position of the text contents on the textbox to make it beautiful. This article is going to introduce how to set the inner margins of the textbox in Excel worksheet in C#.

using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Shapes;
namespace SetInternalMargin
{
    class Program
    {
        static void Main(string[] args)
        {
            {

                //load the sample document
                Workbook workbook = new Workbook();
                workbook.LoadFromFile("Sample.xlsx", ExcelVersion.Version2010);

                //get the first worksheet
                Worksheet sheet = workbook.Worksheets[0];

                //add a textbox to the sheet and set its position and size
                XlsTextBoxShape textbox = sheet.TextBoxes.AddTextBox(4, 2, 100, 300) as XlsTextBoxShape;

                //set the text on the textbox
                textbox.Text = "Insert TextBox in Excel and set the margin for the text";
                textbox.HAlignment = CommentHAlignType.Center;
                textbox.VAlignment = CommentVAlignType.Center;

                //set the inner margins of the contents 
                textbox.InnerLeftMargin = 1;
                textbox.InnerRightMargin = 3;
                textbox.InnerTopMargin = 1;
                textbox.InnerBottomMargin = 1;

                //save the document to file
                workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);

            }

        }
    }
}

Effective screenshot after setting the margins of the contents:

Set the internal margin of excel textbox in C#

Get Cell Type in Excel in C#

2025-04-23 06:35:00 Written by Koohji

When working with Excel files programmatically in C#, determining the data type of a cell is critical for accurate data processing. Whether you're validating user inputs, parsing spreadsheets, or migrating data, knowing how to get the cell data types ensures your application handles information correctly.

In this guide, you'll learn how to use Spire.XLS for .NET library to check the cell data types in Excel in C#, covering the following aspects:

Install the .NET Excel Library

Before proceeding, you need to have the Spire.XLS for .NET library installed in your project. A recommended way to install it is via NuGet, and there are two available options:

  • 1.Right-click the project in “Solution Explorer -> Manage NuGet Packages”. Search for “Spire.XLS” and install the latest version.
  • 2.Go to “Tools -> NuGet Package Manager -> Package Manager Console”, and then run the following command:
    PM> Install-Package Spire.XLS

Alternatively, you can download the library from this link and manually add the DLL files as references.

Learn the Cell Data Types in Spire.XLS

Spire.XLS provides the XlsWorksheet.TRangeValueType enumeration to represents the cell value types. It includes following members:

Cell Type Description
String Represents the cells that store alphanumeric characters, including numbers stored as text.
Number Represents the cells that contain numeric value, including integers, decimals, scientific notation, and date-time values.
Formula Represents the cells that contain a formula.
Blank Represents the cells contain no data.
Boolean Represents the cells that contain TRUE or FALSE.
Error Represents the cells may display errors as #VALUE! or #N/A due to invalid operations.

How to Get Cell Data Type in Excel in C#

To check the value type of a cell, you need to follow the below steps.

  • Create a Workbook object.
  • Load an XLS or XLSX Excel file, and then get a specified worksheet within it.
  • Get a specified cell range in the sheet though the Worksheet.Range[] property.
  • Iterate through each cell in the cell range.
  • Get the row number and column number of the current cell.
  • Call the Worksheet.GetCellType(int row, int column, bool bNeedFormulaSubType) method to get the value type of the current cell, and return a specific enumeration member of the XlsWorksheet.TRangeValueType enumeration type.
  • Converts the value of the enumeration member to its corresponding text string.
  • Write the text string to another cell and set its font style.
  • Save the result file using the Workbook.SaveToFile() method.
  • Sample C# Code
using System.Drawing;
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet;

namespace GetCellType
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Workbook object
            Workbook workbook = new Workbook();
            // Load an Excel file
            workbook.LoadFromFile("Test1.xlsx");

            // Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            // Get a specified cell range
            CellRange range = sheet.Range["A2:A8"];

            // Iterate through all cells in the range
            foreach (CellRange cell in range)
            {
                // Get the row number and column number of the current cell
                int row = cell.Row;
                int column = cell.Column;

                // Get the value type of the current cell
                XlsWorksheet.TRangeValueType cellType = sheet.GetCellType(row, column, false);

                // Convert the cell type value to a text string and write to another cell
                sheet[row, column + 1].Text = cellType.ToString();

                // Set the font style of the output cell
                sheet[row, column + 1].Style.Font.Color = Color.Red;
                sheet[row, column + 1].Style.Font.IsBold = true;
            }

            // Save the result file
            workbook.SaveToFile("GetCellType.xlsx", ExcelVersion.Version2016);
        }
    }
}

A screenshot of the result file:

Get or retrieve the cell data types in an Excel worksheet

Conclusion

Spire.XLS simplifies checking Excel cell data types in C# with its intuitive API. By leveraging the GetCellType() method, you can accurately determine whether a cell contains a number, string, boolean, etc. This approach ensures robust data processing for applications that interact with Excel files.

Get a Free License

To fully experience the capabilities of Spire.XLS for .NET without any evaluation limitations, you can request a free 30-day trial license.

page 156