Skip to content

Commit 107639a

Browse files
Merge pull request #14 from shoaibkhan-aspose/master
Aspose.Cells Java vs Apache POI HSSF + XSSF v1.4
2 parents b67cbc0 + ae0c0da commit 107639a

File tree

12 files changed

+803
-0
lines changed

12 files changed

+803
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package featurescomparison.workingwithcellsrowscolumns.addcomments.java;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
7+
import org.apache.poi.ss.usermodel.Cell;
8+
import org.apache.poi.ss.usermodel.ClientAnchor;
9+
import org.apache.poi.ss.usermodel.Comment;
10+
import org.apache.poi.ss.usermodel.CreationHelper;
11+
import org.apache.poi.ss.usermodel.Drawing;
12+
import org.apache.poi.ss.usermodel.RichTextString;
13+
import org.apache.poi.ss.usermodel.Row;
14+
import org.apache.poi.ss.usermodel.Sheet;
15+
import org.apache.poi.ss.usermodel.Workbook;
16+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
17+
18+
19+
public class ApacheAddCommentsToCell
20+
{
21+
public static void main(String[] args) throws IOException
22+
{
23+
String dataPath = "src/featurescomparison/workingwithcellsrowscolumns/addcomments/data/";
24+
25+
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
26+
27+
CreationHelper factory = wb.getCreationHelper();
28+
29+
Sheet sheet = wb.createSheet();
30+
31+
Row row = sheet.createRow(3);
32+
Cell cell = row.createCell(5);
33+
cell.setCellValue("F4");
34+
35+
Drawing drawing = sheet.createDrawingPatriarch();
36+
37+
// When the comment box is visible, have it show in a 1x3 space
38+
ClientAnchor anchor = factory.createClientAnchor();
39+
anchor.setCol1(cell.getColumnIndex());
40+
anchor.setCol2(cell.getColumnIndex()+1);
41+
anchor.setRow1(row.getRowNum());
42+
anchor.setRow2(row.getRowNum()+3);
43+
44+
// Create the comment and set the text+author
45+
Comment comment = drawing.createCellComment(anchor);
46+
RichTextString str = factory.createRichTextString("Hello, World!");
47+
comment.setString(str);
48+
comment.setAuthor("Apache POI");
49+
50+
// Assign the comment to the cell
51+
cell.setCellComment(comment);
52+
53+
String fname = "AsposeComment-xssf.xls";
54+
if(wb instanceof XSSFWorkbook) fname += "x";
55+
FileOutputStream out = new FileOutputStream(dataPath + fname);
56+
wb.write(out);
57+
out.close();
58+
System.out.println("Done.");
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package featurescomparison.workingwithcellsrowscolumns.addcomments.java;
2+
3+
import com.aspose.cells.Comment;
4+
import com.aspose.cells.Workbook;
5+
import com.aspose.cells.Worksheet;
6+
7+
public class AsposeAddCommentsToCell
8+
{
9+
public static void main(String[] args) throws Exception
10+
{
11+
String dataPath = "src/featurescomparison/workingwithcellsrowscolumns/addcomments/data/";
12+
13+
//Instantiating a Workbook object
14+
Workbook workbook = new Workbook();
15+
16+
Worksheet worksheet = workbook.getWorksheets().get(0);
17+
18+
//Adding a comment to "F5" cell
19+
int commentIndex = worksheet.getComments().add("F5");
20+
Comment comment = worksheet.getComments().get(commentIndex);
21+
22+
//Setting the comment note
23+
comment.setNote("Hello Aspose!");
24+
25+
//Saving the Excel file
26+
workbook.save(dataPath + "AsposeComments.xls");
27+
28+
System.out.println("Done.");
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* ====================================================================
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
==================================================================== */
17+
18+
package featurescomparison.workingwithcellsrowscolumns.splitpanes.java;
19+
20+
import org.apache.poi.ss.usermodel.Workbook;
21+
import org.apache.poi.ss.usermodel.Sheet;
22+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
23+
24+
import java.io.FileOutputStream;
25+
26+
/**
27+
* How to set split panes
28+
*/
29+
public class ApacheSplitPanes
30+
{
31+
public static void main(String[]args) throws Exception
32+
{
33+
34+
String dataPath = "src/featurescomparison/workingwithcellsrowscolumns/splitpanes/data/";
35+
36+
Workbook wb = new XSSFWorkbook();
37+
Sheet sheet = wb.createSheet("new sheet");
38+
39+
// Create a split with the lower left side being the active quadrant
40+
sheet.createSplitPane(2000, 2000, 0, 0, Sheet.PANE_LOWER_LEFT);
41+
42+
FileOutputStream fileOut = new FileOutputStream(dataPath + "ApacheSplitFreezePanes.xlsx");
43+
wb.write(fileOut);
44+
fileOut.close();
45+
System.out.println("Done.");
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package featurescomparison.workingwithcellsrowscolumns.splitpanes.java;
2+
3+
import com.aspose.cells.SaveFormat;
4+
import com.aspose.cells.Workbook;
5+
6+
public class AsposeSplitPanes
7+
{
8+
public static void main(String[] args) throws Exception
9+
{
10+
String dataPath = "src/featurescomparison/workingwithcellsrowscolumns/splitpanes/data/";
11+
12+
//Instantiate a new workbook / Open a template file
13+
Workbook book = new Workbook(dataPath + "workbook.xls");
14+
15+
//Set the active cell
16+
book.getWorksheets().get(0).setActiveCell("A20");
17+
18+
//Split the worksheet window
19+
book.getWorksheets().get(0).split();
20+
21+
//Save the Excel file
22+
book.save(dataPath + "AsposeSplitPanes.xls", SaveFormat.EXCEL_97_TO_2003);
23+
24+
System.out.println("Done.");
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* ====================================================================
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
==================================================================== */
17+
package featurescomparison.workingwithformattingfeatures.cellalignment.java;
18+
19+
import org.apache.poi.hssf.usermodel.*;
20+
21+
import java.io.FileOutputStream;
22+
import java.io.IOException;
23+
24+
/**
25+
* Shows how various alignment options work.
26+
*
27+
* @author Glen Stampoultzis (glens at apache.org)
28+
*/
29+
public class ApacheCellAlignment
30+
{
31+
public static void main(String[] args) throws IOException
32+
{
33+
String dataPath = "src/featurescomparison/workingwithformattingfeatures/cellalignment/data/";
34+
35+
HSSFWorkbook wb = new HSSFWorkbook();
36+
HSSFSheet sheet = wb.createSheet("new sheet");
37+
HSSFRow row = sheet.createRow(2);
38+
createCell(wb, row, 0, HSSFCellStyle.ALIGN_CENTER);
39+
createCell(wb, row, 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
40+
createCell(wb, row, 2, HSSFCellStyle.ALIGN_FILL);
41+
createCell(wb, row, 3, HSSFCellStyle.ALIGN_GENERAL);
42+
createCell(wb, row, 4, HSSFCellStyle.ALIGN_JUSTIFY);
43+
createCell(wb, row, 5, HSSFCellStyle.ALIGN_LEFT);
44+
createCell(wb, row, 6, HSSFCellStyle.ALIGN_RIGHT);
45+
46+
// Write the output to a file
47+
FileOutputStream fileOut = new FileOutputStream(dataPath + "ApahceAlignment.xls");
48+
wb.write(fileOut);
49+
fileOut.close();
50+
System.out.println("Done.");
51+
}
52+
53+
/**
54+
* Creates a cell and aligns it a certain way.
55+
*
56+
* @param wb the workbook
57+
* @param row the row to create the cell in
58+
* @param column the column number to create the cell in
59+
* @param align the alignment for the cell.
60+
*/
61+
private static void createCell(HSSFWorkbook wb, HSSFRow row, int column, int align) {
62+
HSSFCell cell = row.createCell(column);
63+
cell.setCellValue("Align It");
64+
HSSFCellStyle cellStyle = wb.createCellStyle();
65+
cellStyle.setAlignment((short)align);
66+
cell.setCellStyle(cellStyle);
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package featurescomparison.workingwithformattingfeatures.cellalignment.java;
2+
3+
import com.aspose.cells.Cell;
4+
import com.aspose.cells.Cells;
5+
import com.aspose.cells.Style;
6+
import com.aspose.cells.TextAlignmentType;
7+
import com.aspose.cells.Workbook;
8+
import com.aspose.cells.Worksheet;
9+
10+
public class AsposeCellAlignment
11+
{
12+
public static void main(String[] args) throws Exception
13+
{
14+
String dataPath = "src/featurescomparison/workingwithformattingfeatures/cellalignment/data/";
15+
16+
//Instantiating a Workbook object
17+
Workbook workbook = new Workbook();
18+
19+
//Accessing the added worksheet in the Excel file
20+
int sheetIndex = workbook.getWorksheets().add();
21+
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
22+
Cells cells = worksheet.getCells();
23+
24+
//Adding the current system date to "A1" cell
25+
Cell cell = cells.get("A1");
26+
Style style = cell.getStyle();
27+
28+
//Adding some value to the "A1" cell
29+
cell.setValue("Visit Aspose!");
30+
31+
//Setting the horizontal alignment of the text in the "A1" cell
32+
style.setHorizontalAlignment(TextAlignmentType.CENTER);
33+
34+
//Saved style
35+
cell.setStyle(style);
36+
37+
//Saving the modified Excel file in default format
38+
workbook.save(dataPath + "AsposeCellsAlignment.xls");
39+
40+
System.out.println("Done.");
41+
}
42+
}

0 commit comments

Comments
 (0)