Cells for PHP: how to apply a border and a color to a cell

Hi,

I’m searching a solution for 10 hour, no answer found on internet: I’m using Aspose Cells for Java in a PHP environment, thanks to Java Bridge.

I simply want to
- apply a background color to a cell
- apply a border to a cell (on the right side for example)
- apply a color to this border.

Many source code for Java, but unable to do the same with the PHP wrapper.

Here is my code:

// no effect
$this->worksheet->getCells()->getCell(3,5)->getStyle()->setBorderLine(BorderType_BOTTOM, BorderLineType_THICK);

// Bug: how I instanciate/use colors ?? $this->worksheet->getCells()->getCell(3,5)->getStyle()->setBorderColor(BorderType_BOTTOM, new Cells\Color(Color_RED));


Maybe someone can help me?

Thanks,
Damien

Hi,

Thanks for your question and using Aspose.Cells for Java.

Please see the following Java code and its output xlsx file attached with this post.

The code sets the fill color to red, font color to blue and sets the bottom border as THICK border. Please see the screenshot below for your reference.

Please note, you need to use Cell.getStyle() and Cell.setStyle() methods to apply color and border settings.

Please convert the following code into PHP code as illustrated in this article. If you still face problem, please let us know, we will help you asap.


Java

//Create a workbook
Workbook workbook = new Workbook();

//Access the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

//Access cell A1 and put some text value
Cell a1 = worksheet.getCells().get("A1");
a1.putValue("Hello Aspose!");

//Set fill color, font color and bottom border
Style st = a1.getStyle();
st.setPattern(BackgroundType.SOLID);
st.setForegroundColor(Color.getRed());
st.getFont().setColor(Color.getBlue());
st.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).setLineStyle(CellBorderType.THICK);
a1.setStyle(st);

//Autofit columns
worksheet.autoFitColumns();

//Save the workbook
workbook.save("Output.xlsx", SaveFormat.XLSX);

Screenshot:

Hi Shakeel,

Many thanks for this prompt reply.

As explained, this part (background & borders) of Aspose Cells for Java

  • is well documented
  • source code can be found easily on internet

My problem is focused on the PHP wrapper methods. I’m a true friend of google, but I’m unable to

  • fin a documentation of the wrapper for theses methodes (witch function to use, what are the argument, etc.). They slightly differs to the java version (the functions name are not the same, the arguments are not the same).
  • find any source code using these methods with the PHP wrapper

Here are the constants and functions witch seems useful in the asposeCells.php wrapper:

define (“BorderLineType_THICK”,5);

define (“BorderType_RIGHT”,1);

define (“Color_RED”,“Color [R=255, G=0, B=0, TINT=0.0, THEME=-1]”);

class Style {

/* @param pInt0 int / @param oColor1 Color /
function setBorderColor($pInt0, $oColor1)

/ @param pInt0 int / @param pInt1 int */
function setBorderLine($pInt0, $pInt1)

}

As you can see in my first post, I use getStyle() to access border properties (regarding your sentence in red).

Same problem (I think), theses line give no effect on resulting spreadsheet (ignored ??):

$this->worksheet->getCells()->getCell(1,1)->getStyle()->getFont()->setBold(true);
$this->worksheet->getCells()->getCell(1,1)->getStyle()->getFont()->setsize(17);


It will be great if you can propose a PHP equivalent of my/your source code.

Regards,

Damien



Hi,

Thanks for your posting.

Please find all the wrappers inside this file AsposeCells.php, it will give you idea which overloaded method (with serial number) is to be used.

For example, workbook constructors look like this: createXXXWorkbook where XXX is a number

create new wrapped class [Workbook] for java version [com.aspose.cellsWorkbook]
static function createWorkbook()

create new wrapped class [Workbook] for java version [com.aspose.cellsWorkbook]
static function create1Workbook($pInt0)

create new wrapped class [Workbook] for java version [com.aspose.cellsWorkbook]
static function create2Workbook($oInputStream0)

create new wrapped class [Workbook] for java version [com.aspose.cellsWorkbook]
static function create3Workbook($oString0)

create new wrapped class [Workbook] for java version [com.aspose.cellsWorkbook]
static function create4Workbook($oInputStream0, $oLoadOptions1)

create new wrapped class [Workbook] for java version [com.aspose.cellsWorkbook]
static function create5Workbook($oString0, $oLoadOptions1)

Let us know if you still need any help.

Hi,

Thanks for this answer, but I perfectly know this wrapper file. In my previous answer, I have quoted some constants/methods provided by this wrapper file.

As said, I know

  • How to adjust borders/background color etc. in JAVA
  • Method witch seems available in the wrapper for my needs (see my previous post)

My problem: I don’t know how to use them, as explained in my first post and the next one: no documentation for theses methods, no example source code.

Put a background color and a right border to a cell in PHP seems a basic need, but let me know if I have to pay to get a five-line PHP working source code, it’s quite urgent and strategic for me.

Regards,

Damien


Hi,

Normally, it is very easy to convert Java code to PHP code or Coldfusion code.

But since, you are facing a trouble converting it, so I have forwarded your query to development team and logged this issue in our database.

We will soon look into your requirements and provide you PHP equivalent code which I shared above in Java. Once there is some update for you, we will let you know asap.

This issue has been logged as CELLSJAVA-40293.

Hi,

Thanks for using Aspose.Cells.

Download Link:
Aspose.Cells for Java (Latest Version)


It seems you are using some old versions. Because we have made many changes for the APIs of cells component from v7.0.0, we recommend you to use our latest version/fix of aspose.cells and attached new php wrapper. And the code to set cell’s border and color is like following:

PHP


require(“AsposeCells.php”);


$workbook = ClassFactory::createWorkbook();

$cell = $workbook->getWorksheets()->getI(0)->getCells()->getII(0, 0);

$cell->putValueS(“teststring”);

$style = $cell->getStyle();

$color = ClassFactory::createColor();

//set border

$style->setBorder(BorderType_BOTTOM_BORDER, CellBorderType_THICK, $color->fromArgbIII(255, 0, 0));

//set fill

$style->setPattern(BackgroundType_SOLID);

$style->setForegroundColor($color->fromArgbIII(0,255,0));

//apply style to cell

$cell->setStyleS($style);

$workbook->saveS(“res.xls”);