Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This Gist contains code snippets from examples of Aspose.Cells for Java.
This gist exceeds the recommended number of files (~10). To access all files, please clone this gist.
This Gist contains code snippets from examples of Aspose.Cells for Java.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ImportingFromJson.class) + "Data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.getWorksheets().get(0);
// Read File
File file = new File(dataDir + "Test.json");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String jsonInput = "";
String tempString;
while ((tempString = bufferedReader.readLine()) != null) {
jsonInput = jsonInput + tempString;
}
bufferedReader.close();
// Set Styles
CellsFactory factory = new CellsFactory();
Style style = factory.createStyle();
style.setHorizontalAlignment(TextAlignmentType.CENTER);
style.getFont().setColor(Color.getBlueViolet());
style.getFont().setBold(true);
// Set JsonLayoutOptions
JsonLayoutOptions options = new JsonLayoutOptions();
options.setTitleStyle(style);
options.setArrayAsTable(true);
// Import JSON Data
JSONUtility.importData(jsonInput, worksheet.getCells(), 0, 0, options);
// Save Excel file
workbook.save(dataDir + "ImportingFromJson.out.xlsx");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ConvertTableToRangeWithOptions.class) + "LoadingSavingConvertingAndManaging/";
// Open an existing file that contains a table/list object in it
Workbook workbook = new Workbook(dataDir + "KeepSeparatorsForBlankRow.xlsx");
// Instantiate Text File's Save Options
TxtSaveOptions options = new TxtSaveOptions();
// Set KeepSeparatorsForBlankRow to true show separators in blank rows
options.setKeepSeparatorsForBlankRow(true);
// Save the file with the options
workbook.save(dataDir + "KeepSeparatorsForBlankRow.out.csv", options);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Source directory
String dataDir = Utils.getSharedDataDir(OpeningCSVFilesAndReplacingInvalidCharacters.class) + "LoadingSavingConvertingAndManaging/";
LoadOptions loadOptions = new LoadOptions(FileFormatType.CSV);
//Load CSV file
Workbook workbook = new Workbook(dataDir + "[20180220142533][ASPOSE_CELLS_TEST].csv", loadOptions);
System.out.println(workbook.getWorksheets().get(0).getName()); // (20180220142533)(ASPOSE_CELLS_T
System.out.println(workbook.getWorksheets().get(0).getName().length()); // 31
System.out.println("CSV file opened successfully!");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AutofitRowsforMergedCells.class) + "RowsAndColumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create a range A1:B1
Range range = worksheet.getCells().createRange(0, 0, 1, 2);
// Merge the cells
range.merge();
// Insert value to the merged cell A1
worksheet.getCells().get(0, 0).setValue("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end");
// Create a style object
Style style = worksheet.getCells().get(0, 0).getStyle();
// Set wrapping text on
style.setTextWrapped(true);
// Apply the style to the cell
worksheet.getCells().get(0, 0).setStyle(style);
// Create an object for AutoFitterOptions
AutoFitterOptions options = new AutoFitterOptions();
// Set auto-fit for merged cells
options.setAutoFitMergedCellsType(AutoFitMergedCellsType.EACH_LINE);
// Autofit rows in the sheet(including the merged cells)
worksheet.autoFitRows(options);
// Save the Excel file
workbook.save(dataDir + "AutofitRowsforMergedCells_out.xlsx");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getSharedDataDir(InsertingARowWithFormatting.class) + "RowsAndColumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Setting Formatting options
InsertOptions insertOptions = new InsertOptions();
insertOptions.setCopyFormatType(CopyFormatType.SAME_AS_ABOVE);
// Inserting a row into the worksheet at 3rd position
worksheet.getCells().insertRows(2, 1, insertOptions);
// Saving the modified Excel file
workbook.save(dataDir + "InsertingARowWithFormatting_out.xlsx");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ConvertTableToRangeWithOptions.class) + "Tables/";
// Open an existing file that contains a table/list object in it
Workbook workbook = new Workbook(dataDir + "book1.xlsx");
TableToRangeOptions options = new TableToRangeOptions();
options.setLastRow(5);
// Convert the first table/list object (from the first worksheet) to normal range
workbook.getWorksheets().get(0).getListObjects().get(0).convertToRange(options);
// Save the file
workbook.save(dataDir + "ConvertTableToRangeWithOptions_out.xlsx");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AutoFitRowsforMergedCells.class) + "TechnicalArticles/";
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first (default) worksheet
Worksheet _worksheet = workbook.getWorksheets().get(0);
// Create a range A1:B1
Range range = _worksheet.getCells().createRange(0, 0, 1, 2);
// Merge the cells
range.merge();
// Insert value to the merged cell A1
_worksheet.getCells().get(0, 0).setValue(
"A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end");
// Create a style object
Style style = _worksheet.getCells().get(0, 0).getStyle();
// Set wrapping text on
style.setTextWrapped(true);
// Apply the style to the cell
_worksheet.getCells().get(0, 0).setStyle(style);
// Create an object for AutoFitterOptions
AutoFitterOptions options = new AutoFitterOptions();
// Set auto-fit for merged cells
options.setAutoFitMergedCellsType(AutoFitMergedCellsType.EACH_LINE);
// Autofit rows in the sheet(including the merged cells)
_worksheet.autoFitRows(options);
// Save the Excel file
workbook.save(dataDir + "AFRFMergedCells.xlsx");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Source directory
String dataDir = AsposeCellsExamples.Utils.getSharedDataDir(DetectFileFormatOfEncryptedFiles.class) + "TechnicalArticles/";
String filename = dataDir + "encryptedBook1.out.tmp";
FileFormatInfo fileFormatInfo = FileFormatUtil.detectFileFormat(filename,"1234"); // The password is 1234
if(fileFormatInfo.getFileFormatType() == FileFormatType.EXCEL_97_TO_2003) {
System.out.println("File Format: EXCEL_97_TO_2003");
} else if(fileFormatInfo.getFileFormatType() == FileFormatType.PPTX) {
System.out.println("File Format: PPTX");
} else if(fileFormatInfo.getFileFormatType() == FileFormatType.DOCX) {
System.out.println("File Format: DOCX");
}
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(sourceDir + "HyperlinksSample.xlsx");
// Get the first (default) worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create a range A2:B3
Range range = worksheet.getCells().createRange("A2", "B3");
// Get Hyperlinks in range
Hyperlink[] hyperlinks = range.getHyperlinks();
for (Hyperlink link : hyperlinks){
System.out.println(link.getArea() + " : " + link.getAddress());
// To delete the link, use the Hyperlink.Delete() method.
link.delete();
}
workbook.save(outputDir + "HyperlinksSample_out.xlsx");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Define variables
String name = "HasFormula";
String value = "=GET.CELL(48, INDIRECT(\"ZS\",FALSE))";
// Load the template file
Workbook wbSource = new Workbook(srcDir + "sampleNamedRangeTest.xlsm");
// Get the worksheets collection
WorksheetCollection wsCol = wbSource.getWorksheets();
// Add new name to the names collection
int nameIndex = wsCol.getNames().add(name);
// Set value to the named range
Name namedRange = wsCol.getNames().get(nameIndex);
namedRange.setRefersTo(value);
// Save the output file
wbSource.save(outDir + "sampleOutputNamedRangeTest.xlsm");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CountNumberOfCells.class) + "Worksheets/";
//Load source Excel file
Workbook workbook = new Workbook(dataDir + "BookWithSomeData.xlsx");
//Access first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
//Print number of cells in the Worksheet
System.out.println("Number of Cells: " + worksheet.getCells().getCount());
// If the number of cells is greater than 2147483647, use CountLarge
System.out.println("Number of Cells (CountLarge): " + worksheet.getCells().getCountLarge());
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SaveFileInExcel972003format.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook();
// Save in Excel2003 format
workbook.save(dataDir + "output.xls", FileFormatType.EXCEL_97_TO_2003);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SaveInExcel2007xlsbFormat.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook();
// Save in Excel2007 xlsb format
workbook.save(dataDir + "output.xlsb", FileFormatType.XLSB);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SaveInExcel2007xlsxFormat.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook();
// Save in Excel2007 xlsx format
workbook.save(dataDir + "output.xlsx", FileFormatType.XLSX);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SaveInHtmlFormat.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook();
// Save in SpreadsheetML format
workbook.save(dataDir + "output.html", FileFormatType.HTML);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SaveInODSFormat.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook();
// Save in ODS format
workbook.save(dataDir + "output.ods", FileFormatType.ODS);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SaveInPdfFormat.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook();
// Save in PDF format
workbook.save(dataDir + "output.pdf", FileFormatType.PDF);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SaveInSpreadsheetMLFormat.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook();
// Save in SpreadsheetML format
workbook.save(dataDir + "output.xml", FileFormatType.EXCEL_2003_XML);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SaveWorkbookToTextCSVFormat.class);
// Load your source workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// 0-byte array
byte[] workbookData = new byte[0];
// Text save options. You can use any type of separator
TxtSaveOptions opts = new TxtSaveOptions();
opts.setSeparator('\t');
// Copy each worksheet data in text format inside workbook data array
for (int idx = 0; idx < workbook.getWorksheets().getCount(); idx++) {
// Save the active worksheet into text format
ByteArrayOutputStream bout = new ByteArrayOutputStream();
workbook.getWorksheets().setActiveSheetIndex(idx);
workbook.save(bout, opts);
// Save the worksheet data into sheet data array
byte[] sheetData = bout.toByteArray();
// Combine this worksheet data into workbook data array
byte[] combinedArray = new byte[workbookData.length + sheetData.length];
System.arraycopy(workbookData, 0, combinedArray, 0, workbookData.length);
System.arraycopy(sheetData, 0, combinedArray, workbookData.length, sheetData.length);
workbookData = combinedArray;
}
// Save entire workbook data into file
FileOutputStream fout = new FileOutputStream(dataDir + "data.out.txt");
fout.write(workbookData);
fout.close();
// Print message
System.out.println("Excel to Text File Conversion performed successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SaveXLSFile.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook();
// Save in xls format
workbook.save(dataDir + "output.xls", FileFormatType.EXCEL_97_TO_2003);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SaveXLSXFile.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook();
// Save in xlsx format
workbook.save(dataDir + "output.xlsx", FileFormatType.XLSX);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SavingFiles.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook(dataDir + "book.xls");
// Save in default (Excel2003) format
workbook.save(dataDir + "book.default.out.xls");
// Save in Excel2003 format
workbook.save(dataDir + "book.out.xls", FileFormatType.EXCEL_97_TO_2003);
// Save in Excel2007 xlsx format
workbook.save(dataDir + "book.out.xlsx", FileFormatType.XLSX);
// Save in SpreadsheetML format
workbook.save(dataDir + "book.out.xml", FileFormatType.EXCEL_2003_XML);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SavingFiletoSomeLocation.class);
String filePath = dataDir + "Book1.xls";
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook(filePath);
// Save in Excel 97 – 2003 format
workbook.save(dataDir + ".output.xls");
// OR workbook.save(dataDir + ".output..xls", new XlsSaveOptions(SaveFormat.Excel97To2003));
// Save in Excel2007 xlsx format
workbook.save(dataDir + ".output.xlsx", FileFormatType.XLSX);
// Save in Excel2007 xlsb format
workbook.save(dataDir + ".output.xlsb", FileFormatType.XLSB);
// Save in ODS format
workbook.save(dataDir + ".output.ods", FileFormatType.ODS);
// Save in Pdf format
workbook.save(dataDir + ".output.pdf", FileFormatType.PDF);
// Save in Html format
workbook.save(dataDir + ".output.html", FileFormatType.HTML);
// Save in SpreadsheetML format
workbook.save(dataDir + ".output.xml", FileFormatType.EXCEL_2003_XML);
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SavingFiletoStream.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
FileOutputStream stream = new FileOutputStream(dataDir + "output.xlsx");
workbook.save(stream, new XpsSaveOptions(FileFormatType.XLSX));
// Print Message
System.out.println("Worksheets are saved successfully.");
stream.close();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SavingTextFilewithCustomSeparator.class);
// Creating an Workbook object with an Excel file path
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
TxtSaveOptions toptions = new TxtSaveOptions();
// Specify the separator
toptions.setSeparator(';');
workbook.save(dataDir + "output.csv");
// Print Message
System.out.println("Worksheets are saved successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(AdvancedConversiontoPdf.class);
// Create a new Workbook.
Workbook workbook = new Workbook();
Cells cell = workbook.getWorksheets().get(0).getCells();
cell.get("A12").setValue("Test PDF");
PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.setCompliance(PdfCompliance.PDF_A_1_B);
workbook.save(dataDir + "output2.pdf", pdfOptions);
// Print message
System.out.println("Advanced Conversion performed successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ChartToImage.class);
// Create a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
// Set the name of worksheet
sheet.setName("Data");
// Get the cells collection in the sheet.
Cells cells = workbook.getWorksheets().get(0).getCells();
// Put some values into a cells of the Data sheet.
cells.get("A1").setValue("Region");
cells.get("A2").setValue("France");
cells.get("A3").setValue("Germany");
cells.get("A4").setValue("England");
cells.get("A5").setValue("Sweden");
cells.get("A6").setValue("Italy");
cells.get("A7").setValue("Spain");
cells.get("A8").setValue("Portugal");
cells.get("B1").setValue("Sale");
cells.get("B2").setValue(70000);
cells.get("B3").setValue(55000);
cells.get("B4").setValue(30000);
cells.get("B5").setValue(40000);
cells.get("B6").setValue(35000);
cells.get("B7").setValue(32000);
cells.get("B8").setValue(10000);
// Create chart
int chartIndex = sheet.getCharts().add(ChartType.COLUMN, 12, 1, 33, 12);
Chart chart = sheet.getCharts().get(chartIndex);
// Set properties of chart title
chart.getTitle().setText("Sales By Region");
chart.getTitle().getFont().setBold(true);
chart.getTitle().getFont().setSize(12);
// Set properties of nseries
chart.getNSeries().add("Data!B2:B8", true);
chart.getNSeries().setCategoryData("Data!A2:A8");
// Set the fill colors for the series's data points (France - Portugal(7 points))
ChartPointCollection chartPoints = chart.getNSeries().get(0).getPoints();
ChartPoint point = chartPoints.get(0);
point.getArea().setForegroundColor(Color.getCyan());
point = chartPoints.get(1);
point.getArea().setForegroundColor(Color.getBlue());
point = chartPoints.get(2);
point.getArea().setForegroundColor(Color.getYellow());
point = chartPoints.get(3);
point.getArea().setForegroundColor(Color.getRed());
point = chartPoints.get(4);
point.getArea().setForegroundColor(Color.getBlack());
point = chartPoints.get(5);
point.getArea().setForegroundColor(Color.getGreen());
point = chartPoints.get(6);
point.getArea().setForegroundColor(Color.getMaroon());
// Set the legend invisible
chart.setShowLegend(false);
// Get the Chart image
ImageOrPrintOptions imgOpts = new ImageOrPrintOptions();
imgOpts.setImageFormat(ImageFormat.getEmf());
FileOutputStream fs = new FileOutputStream(dataDir + "output.emf");
// Save the chart image file.
chart.toImage(fs, imgOpts);
fs.close();
// Print message
System.out.println("Processing performed successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(ConvertChartToPdf.class);
String inputPath = dataDir + "Sample1.xls";
String outputPath = dataDir + "Output.pdf";
// Load excel file containing charts
Workbook workbook = new Workbook(inputPath);
// Access first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Access first chart inside the worksheet
Chart chart = worksheet.getCharts().get(0);
// Save the chart into pdf format
chart.toPdf(outputPath);
System.out.println("File saved " + outputPath);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ConvertingToHTMLFiles.class);
// Specify the file path
String filePath = dataDir + "Book1.xlsx";
// Specify the HTML saving options
HtmlSaveOptions sv = new HtmlSaveOptions(SaveFormat.M_HTML);
// Instantiate a workbook and open the template XLSX file
Workbook wb = new Workbook(filePath);
// Save the HTML file
wb.save(dataDir + "output.html", sv);
// Print message
System.out.println("Excel to HTML conversion performed successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ConvertingToMHTMLFiles.class);
// Specify the file path
String filePath = dataDir + "Book1.xlsx";
// Specify the HTML saving options
HtmlSaveOptions sv = new HtmlSaveOptions(SaveFormat.M_HTML);
// Instantiate a workbook and open the template XLSX file
Workbook wb = new Workbook(filePath);
// Save the MHT file
wb.save(dataDir + "output.mht", sv);
// Print message
System.out.println("Excel to MHTML conversion performed successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ConvertingToXPS.class);
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
// Apply different Image and Print options
com.aspose.cells.ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set the Format
options.setSaveFormat(SaveFormat.XPS);
// Render the sheet with respect to specified printing options
com.aspose.cells.SheetRender sr = new SheetRender(sheet, options);
sr.toImage(0, dataDir + "output.xps");
// Save the complete Workbook in XPS format
workbook.save(dataDir + "output.xps", SaveFormat.XPS);
// Print message
System.out.println("Excel to XPS conversion performed successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ConvertingWorksheetToSVG.class);
String path = dataDir + "Template.xlsx";
// Create a workbook object from the template file
Workbook workbook = new Workbook(path);
// Convert each worksheet into svg format in a single page.
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setSaveFormat(SaveFormat.SVG);
imgOptions.setOnePagePerSheet(true);
// Convert each worksheet into svg format
int sheetCount = workbook.getWorksheets().getCount();
for (int i = 0; i < sheetCount; i++) {
Worksheet sheet = workbook.getWorksheets().get(i);
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int k = 0; k < sr.getPageCount(); k++) {
// Output the worksheet into Svg image format
sr.toImage(k, path + sheet.getName() + k + ".output.svg");
}
}
// Print message
System.out.println("Excel to SVG conversion completed successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(EncryptingFiles.class);
// Instantiate a Workbook object by excel file path
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Password protect the file.
workbook.getSettings().setPassword("1234");
// Specify XOR encrption type.
workbook.setEncryptionOptions(EncryptionType.XOR, 40);
// Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic Provider).
workbook.setEncryptionOptions(EncryptionType.STRONG_CRYPTOGRAPHIC_PROVIDER, 128);
// Save the excel file.
workbook.save(dataDir + "output.xls");
// Print message
System.out.println("Encryption applied successfully on output file.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(Excel2PDFConversion.class);
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Save the document in PDF format
workbook.save(dataDir + "output.pdf", SaveFormat.PDF);
// Print message
System.out.println("Excel to PDF conversion performed successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the resource directory.
String dataDir = Utils.getSharedDataDir(ExpandTextFromRightToLeftWhileExportingExcelFileToHTML.class) + "Conversion/";
//Load source excel file inside the workbook object
Workbook wb = new Workbook(dataDir + "sample.xlsx");
//Save workbook in HTML format
wb.save(dataDir + "output-" + CellsHelper.getVersion() + ".html", SaveFormat.HTML);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ExportWholeWorkbooktoXPS.class);
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set the format
options.setSaveFormat(SaveFormat.XPS);
// Render the workbook with respect to specified printing options
WorkbookRender render = new WorkbookRender(workbook, options);
render.toImage(dataDir + "output.xps");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the resource directory.
String dataDir = Utils.getSharedDataDir(IsPivotTableCompatibleForExcel2003.class) + "Conversion/";
//Load source excel file containing sample pivot table
Workbook wb = new Workbook(dataDir + "sample-pivot-table.xlsx");
//Access first worksheet that contains pivot table data
Worksheet dataSheet = wb.getWorksheets().get(0);
//Access cell A3 and sets its data
Cells cells = dataSheet.getCells();
Cell cell = cells.get("A3");
cell.putValue("FooBar");
//Access cell B3 and sets its data
//We set B3 a very long string which has more than 255 characters
String longStr = "Very long text 1. very long text 2. very long text 3. very long text 4. very long text 5. very long text 6. very long text 7. very long text 8. very long text 9. very long text 10. very long text 11. very long text 12. very long text 13. very long text 14. very long text 15. very long text 16. very long text 17. very long text 18. very long text 19. very long text 20. End of text.";
cell = cells.get("B3");
cell.putValue(longStr);
//Print the length of cell B3 string
System.out.println("Length of original data string: " + cell.getStringValue().length());
//Access cell C3 and sets its data
cell = cells.get("C3");
cell.putValue("closed");
//Access cell D3 and sets its data
cell = cells.get("D3");
cell.putValue("2016/07/21");
//Access the second worksheet that contains pivot table
Worksheet pivotSheet = wb.getWorksheets().get(1);
//Access the pivot table
PivotTable pivotTable = pivotSheet.getPivotTables().get(0);
//IsExcel2003Compatible property tells if PivotTable is compatible for Excel2003 while refreshing PivotTable.
//If it is true, a string must be less than or equal to 255 characters, so if the string is greater than 255 characters,
//it will be truncated. If false, a string will not have the aforementioned restriction.
//The default value is true.
pivotTable.setExcel2003Compatible(true);
pivotTable.refreshData();
pivotTable.calculateData();
//Check the value of cell B5 of pivot sheet.
//It will be 255 because we have set IsExcel2003Compatible property to true
//All the data after 255 characters has been truncated
Cell b5 = pivotSheet.getCells().get("B5");
System.out.println("Length of cell B5 after setting IsExcel2003Compatible property to True: " + b5.getStringValue().length());
//Now set IsExcel2003Compatible property to false and again refresh
pivotTable.setExcel2003Compatible(false);
pivotTable.refreshData();
pivotTable.calculateData();
//Now it will print 383 the original length of cell data
//The data has not been truncated now.
b5 = pivotSheet.getCells().get("B5");
System.out.println("Length of cell B5 after setting IsExcel2003Compatible property to False: " + b5.getStringValue().length());
//Set the row height and column width of cell B5 and also wrap its text
pivotSheet.getCells().setRowHeight(b5.getRow(), 100);
pivotSheet.getCells().setColumnWidth(b5.getColumn(), 65);
Style st = b5.getStyle();
st.setTextWrapped(true);
b5.setStyle(st);
//Save workbook in xlsx format
wb.save(dataDir + "output.xlsx", SaveFormat.XLSX);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ManagingDocumentProperties.class);
// Instantiate a Workbook object by excel file path
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Retrieve a list of all custom document properties of the Excel file
CustomDocumentPropertyCollection customProperties = workbook.getWorksheets().getCustomDocumentProperties();
// Accessing a custom document property by using the property index
DocumentProperty customProperty1 = customProperties.get(3);
// Accessing a custom document property by using the property name
DocumentProperty customProperty2 = customProperties.get("Owner");
// Adding a custom document property to the Excel file
DocumentProperty publisher = customProperties.add("Publisher", "Aspose");
// Save the file
workbook.save(dataDir + "output.xls");
// Removing a custom document property
customProperties.remove("Publisher");
// Save the file
workbook.save(dataDir + "output.xls");
// Print message
System.out.println("Excel file's custom properties accessed successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(QuickExceltoXPSConversion.class);
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
// Save in XPS format
workbook.save(dataDir + "output.xps", SaveFormat.XPS);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(RenderUnicodeSupplementarycharactersinoutputPdf.class);
// Load your source excel file containing Unicode Supplementary
// characters
Workbook wb = new Workbook(dataDir + "unicode-supplementary-characters.xlsx");
// Save the workbook
wb.save(dataDir + "output.pdf");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(RenderUnicodeSupplementarycharactersinoutputPdf.class);
// Load your source excel file containing Unicode Supplementary
// characters
Workbook wb = new Workbook(dataDir + "unicode-supplementary-characters.xlsx");
// Save the workbook
wb.save(dataDir + "output.pdf");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(RenderUnicodeSupplimentaryCharacterToPDF.class);
// Load your source excel file containing Unicode Supplementary
// characters
Workbook wb = new Workbook(dataDir + "unicode-supplementary-characters.xlsx");
// Save the workbook
wb.save(dataDir + "output.pdf");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(RenderUnicodeSupplimentaryCharacterToPDF.class);
// Load your source excel file containing Unicode Supplementary
// characters
Workbook wb = new Workbook(dataDir + "unicode-supplementary-characters.xlsx");
// Save the workbook
wb.save(dataDir + "output.pdf");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Directory path where output HTML files are to be saved
String dataDir = Utils.getSharedDataDir(SetDefaultFontWhileRenderingSpreadsheetToHTML.class) + "Conversion/";
//Create workbook object.
Workbook wb = new Workbook();
//Access first WorkSheet.
Worksheet ws = wb.getWorksheets().get(0);
//Access cell B4 and add some text inside it.
Cell cell = ws.getCells().get("B4");
cell.putValue("This text has some unknown or invalid font which does not exist.");
//Set the font of cell B4 which is unknown.
Style st = cell.getStyle();
st.getFont().setName("UnknownNotExist");
st.getFont().setSize(20);
cell.setStyle(st);
//Now save the workbook in html format and set the default font to Courier New.
HtmlSaveOptions opts = new HtmlSaveOptions();
opts.setDefaultFontName("Courier New");
wb.save(dataDir + "out_courier_new.htm", opts);
//Now save the workbook in html format once again but set the default font to Arial.
opts.setDefaultFontName("Arial");
wb.save(dataDir + "out_arial.htm", opts);
//Now save the workbook in html format once again but set the default font to Times New Roman.
opts.setDefaultFontName("Times New Roman");
wb.save(dataDir + "out_times_new_roman.htm", opts);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Directory path where output HTML files are to be saved
String dataDir = Utils.getSharedDataDir(SetDefaultFontWhileRenderingSpreadsheetToImages.class) + "Conversion/";
//Create workbook object.
Workbook wb = new Workbook();
//Set default font of the workbook to none
Style s = wb.getDefaultStyle();
s.getFont().setName("");
wb.setDefaultStyle(s);
//Access first worksheet.
Worksheet ws = wb.getWorksheets().get(0);
//Access cell A4 and add some text inside it.
Cell cell = ws.getCells().get("A4");
cell.putValue("This text has some unknown or invalid font which does not exist.");
//Set the font of cell A4 which is unknown.
Style st = cell.getStyle();
st.getFont().setName("UnknownNotExist");
st.getFont().setSize(20);
st.setTextWrapped(true);
cell.setStyle(st);
//Set first column width and fourth column height
ws.getCells().setColumnWidth(0, 80);
ws.getCells().setRowHeight(3, 60);
//Create image or print options.
ImageOrPrintOptions opts = new ImageOrPrintOptions();
opts.setOnePagePerSheet(true);
opts.setImageFormat(ImageFormat.getPng());
//Render worksheet image with Courier New as default font.
opts.setDefaultFont("Courier New");
SheetRender sr = new SheetRender(ws, opts);
sr.toImage(0, dataDir + "out_courier_new.png");
//Render worksheet image again with Times New Roman as default font.
opts.setDefaultFont("Times New Roman");
sr = new SheetRender(ws, opts);
sr.toImage(0, dataDir + "out_times_new_roman.png");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SetPDFCreationTime.class);
// Instantiate a Workbook object by excel file path
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
// Create an instance of PdfSaveOptions and pass SaveFormat to the constructor
PdfSaveOptions options = new PdfSaveOptions(FileFormatType.PDF);
options.setCreatedTime(DateTime.getNow());
// Save the file
workbook.save(dataDir + "output.pdf", options);
// Print message
System.out.println("Set PDF Creation Time successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SettingImagePrefrencesforHTML.class);
// Instantiate a Workbook object by excel file path
Workbook workbook = new Workbook(dataDir + "Book1.xlsx");
System.out.println("Set PDF Creation Time successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(WorksheetToImage.class);
// Instantiate a new workbook with path to an Excel file
Workbook book = new Workbook(dataDir + "MyTestBook1.xls");
// Create an object for ImageOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Set the image type
imgOptions.setImageFormat(ImageFormat.getPng());
// Get the first worksheet.
Worksheet sheet = book.getWorksheets().get(0);
// Create a SheetRender object for the target sheet
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int j = 0; j < sr.getPageCount(); j++) {
// Generate an image for the worksheet
sr.toImage(j, dataDir + "output" + j + ".png");
}
// Print message
System.out.println("Images generated successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(XlstoPDFDirectConversation.class);
// Instantiate a Workbook object by excel file path
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Save the file
workbook.save(dataDir + "output.pdf");
// Print message
System.out.println("Converted xls to Pdf successfully.");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ApplyGradientFillEffects.class);
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first worksheet (default) in the workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
// Input a value into B3 cell
worksheet.getCells().get(2, 1).putValue("test");
// Get the Style of the cell
Style style = worksheet.getCells().get("B3").getStyle();
// Set Gradient pattern on
style.setGradient(true);
// Specify two color gradient fill effects
style.setTwoColorGradient(Color.fromArgb(255, 255, 255), Color.fromArgb(79, 129, 189),
GradientStyleType.HORIZONTAL, 1);
// Set the color of the text in the cell
style.getFont().setColor(Color.getRed());
// Specify horizontal and vertical alignment settings
style.setHorizontalAlignment(TextAlignmentType.CENTER);
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Apply the style to the cell
worksheet.getCells().get("B3").setStyle(style);
// Set the third row height in pixels
worksheet.getCells().setRowHeightPixel(2, 53);
// Merge the range of cells (B3:C3)
worksheet.getCells().merge(2, 1, 1, 2);
// Save the Excel file
workbook.save(dataDir + "Book1.xlsx");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Path to source file
String dataDir = Utils.getDataDir(ConditionalFormattingatRuntime.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
ConditionalFormattingCollection cfs = sheet.getConditionalFormattings();
// The first method:adds an empty conditional formatting
int index = cfs.add();
FormatConditionCollection fcs = cfs.get(index);
// Sets the conditional format range.
CellArea ca1 = new CellArea();
ca1.StartRow = 0;
ca1.StartColumn = 0;
ca1.EndRow = 0;
ca1.EndColumn = 0;
CellArea ca2 = new CellArea();
ca2.StartRow = 0;
ca2.StartColumn = 0;
ca2.EndRow = 0;
ca2.EndColumn = 0;
CellArea ca3 = new CellArea();
ca3.StartRow = 0;
ca3.StartColumn = 0;
ca3.EndRow = 0;
ca3.EndColumn = 0;
fcs.addArea(ca1);
fcs.addArea(ca2);
fcs.addArea(ca3);
// Sets condition formulas.
int conditionIndex = fcs.addCondition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "=A2", "100");
FormatCondition fc = fcs.get(conditionIndex);
int conditionIndex2 = fcs.addCondition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100");
// Saving the Excel file
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(FormattingARow.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Adding a new Style to the styles collection of the Excel object Accessing the newly added Style to the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border of the cell
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBottomBorder(true);
styleFlag.setFontColor(true);
// Accessing a column from the Columns collection
Column column = cells.getColumns().get(0);
// Applying the style to the column
column.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(FormattingARow.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Adding a new Style to the styles collection of the Excel object Accessing the newly added Style to the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Shrinking the text to fit in the cell
style.setShrinkToFit(true);
// Setting the bottom border of the cell
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Creating StyleFlag
StyleFlag styleFlag = new StyleFlag();
styleFlag.setHorizontalAlignment(true);
styleFlag.setVerticalAlignment(true);
styleFlag.setShrinkToFit(true);
styleFlag.setBottomBorder(true);
styleFlag.setFontColor(true);
// Accessing a row from the Rows collection
Row row = cells.getRows().get(0);
// Assigning the Style object to the Style property of the row
row.applyStyle(style, styleFlag);
// Saving the Excel file
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(FormattingCellsUsingsetStyleMethod.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Accessing the "A1" cell from the worksheet
Cell cell = cells.get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello Aspose!");
Style style = cell.getStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the "A1" cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Setting the cell to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(FormattingCellsUsingStyleObject.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
// Accessing the "A1" cell from the worksheet
Cell cell = cells.get("A1");
// Adding some value to the "A1" cell
cell.setValue("Hello Aspose!");
// Adding a new Style to the styles collection of the Excel object
Style style = workbook.createStyle();
// Setting the vertical alignment of the text in the "A1" cell
style.setVerticalAlignment(TextAlignmentType.CENTER);
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Setting the font color of the text in the "A1" cell
Font font = style.getFont();
font.setColor(Color.getGreen());
// Setting the cell to shrink according to the text contained in it
style.setShrinkToFit(true);
// Setting the bottom border
style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.MEDIUM, Color.getRed());
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(FormattingSelectedCharacters.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose!");
Font font = cell.characters(6, 7).getFont();
// Setting the font of selected characters to bold
font.setBold(true);
// Setting the font color of selected characters to blue
font.setColor(Color.getBlue());
// Saving the Excel file
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(Indentation.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the vertical alignment of the text in a cell
Style style1 = cell.getStyle();
style1.setIndentLevel(2);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "book1.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(MakeCellActive.class);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Get the cells in the worksheet.
Cells cells = worksheet1.getCells();
// Input data into B2 cell.
cells.get(1, 1).setValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheetIndex(0);
// Set B2 cell as an active cell in the worksheet.
worksheet1.setActiveCell("B2");
// Set the B column as the first visible column in the worksheet.
worksheet1.setFirstVisibleColumn(1);
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.setFirstVisibleRow(1);
// Save the Excel file.
workbook.save(dataDir + "activecell.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(Orientation.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Merging the first three columns in the first row to create a single cell
cells.merge(0, 0, 1, 3);
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "book1.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(Orientation.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the rotation of the text (inside the cell) to 25
Style style1 = cell.getStyle();
style1.setRotationAngle(25);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "book1.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SelectRangeofCellsinWorksheet.class);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Get the cells in the worksheet.
Cells cells = worksheet1.getCells();
// Input data into B2 cell.
cells.get(1, 1).setValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheetIndex(0);
// Select range of cells(A1:E10) in the worksheet.
worksheet1.selectRange(0, 0, 10, 5, true);
// Save the Excel file.
workbook.save(dataDir + "activecells.xlsx");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Path to source file
String dataDir = Utils.getDataDir(SetBorder.class);
Style style = fc.getStyle();
style.setBorder(BorderType.LEFT_BORDER, CellBorderType.DASHED, Color.fromArgb(0, 255, 255));
style.setBorder(BorderType.TOP_BORDER, CellBorderType.DASHED, Color.fromArgb(0, 255, 255));
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.DASHED, Color.fromArgb(0, 255, 255));
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.DASHED, Color.fromArgb(255, 255, 0));
fc.setStyle(style);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SettingFontName.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting the font color to blue
Style style = cell.getStyle();
Font font = style.getFont();
font.setColor(Color.getBlue());
cell.setStyle(style);
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SettingFontName.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting the font weight to bold
Style style = cell.getStyle();
Font font = style.getFont();
font.setSize(14);
cell.setStyle(style);
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SettingFontName.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting the font weight to bold
Style style = cell.getStyle();
Font font = style.getFont();
font.setBold(true);
cell.setStyle(style);
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Path to source file
String dataDir = Utils.getDataDir(SetBorder.class);
Style style = fc.getStyle();
style.setPattern(BackgroundType.REVERSE_DIAGONAL_STRIPE);
style.setForegroundColor(Color.fromArgb(255, 255, 0));
style.setBackgroundColor(Color.fromArgb(0, 255, 255));
fc.setStyle(style);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Setting subscript effect
Style style = cell.getStyle();
Font font = style.getFont();
font.setSubscript(true);
cell.setStyle(style);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Setting superscript effect
Style style = cell.getStyle();
Font font = style.getFont();
font.setSuperscript(true);
cell.setStyle(style);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SettingDisplayFormat.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first (default) worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(0);
// Adding a new Style to the styles collection of the Workbook object
Style style = workbook.createStyle();
// Setting the Number property to 4 which corresponds to the pattern #,##0.00
style.setNumber(4);
// Creating an object of StyleFlag
StyleFlag flag = new StyleFlag();
// Setting NumberFormat property to true so that only this aspect takes effect from Style object
flag.setNumberFormat(true);
// Applying style to the first row of the worksheet
worksheet.getCells().getRows().get(0).applyStyle(style, flag);
// Re-initializing the Style object
style = workbook.createStyle();
// Setting the Custom property to the pattern d-mmm-yy
style.setCustom("d-mmm-yy");
// Applying style to the first column of the worksheet
worksheet.getCells().getColumns().get(0).applyStyle(style, flag);
// Saving spreadsheet on disc
workbook.save(dataDir + "output.xlsx");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SettingFontName.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting the font name to "Times New Roman"
Style style = cell.getStyle();
Font font = style.getFont();
font.setName("Times New Roman");
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Path to source file
String dataDir = Utils.getDataDir(SettingFontStyle.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
FormatCondition fc;
Style style = fc.getStyle();
Font font = style.getFont();
font.setItalic(true);
font.setBold(true);
font.setStrikeout(true);
font.setUnderline(FontUnderlineType.DOUBLE);
font.setColor(Color.getBlack());
fc.setStyle(style);
// Saving the Excel file
workbook.save(dataDir + "book1.out.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SettingFontName.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding some value to the "A1" cell
Cell cell = cells.get("A1");
cell.setValue("Hello Aspose!");
// Setting the font to be underlined
Style style = cell.getStyle();
Font font = style.getFont();
font.setUnderline(FontUnderlineType.SINGLE);
cell.setStyle(style);
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "output.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// Setting the strike out effect on the font
Style style = cell.getStyle();
Font font = style.getFont();
font.setStrikeout(true);
cell.setStyle(style);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(Orientation.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Shrinking the text to fit according to the dimensions of the cell
Style style1 = cell.getStyle();
style1.setShrinkToFit(true);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "book1.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(TextAlignmentHorizontal.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
style.setHorizontalAlignment(TextAlignmentType.CENTER);
// Saved style
cell.setStyle(style);
// Saving the modified Excel file in default format
workbook.save(dataDir + "book1.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(TextAlignmentVertical.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Setting the vertical alignment of the text in a cell
Style style1 = cell.getStyle();
style1.setVerticalAlignment(TextAlignmentType.CENTER);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "book1.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(Orientation.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells();
// Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
Style style = cell.getStyle();
// Adding some value to the "A1" cell
cell.setValue("Visit Aspose!");
// Enabling the text to be wrapped within the cell
Style style1 = cell.getStyle();
style1.setTextWrapped(true);
cell.setStyle(style1);
// Saved style
cell.setStyle(style1);
// Saving the modified Excel file in default format
workbook.save(dataDir + "book1.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(CalculatingFormulas.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a value to "A1" cell
worksheet.getCells().get("A1").putValue(1);
// Adding a value to "A2" cell
worksheet.getCells().get("A2").putValue(2);
// Adding a value to "A3" cell
worksheet.getCells().get("A3").putValue(3);
// Adding a SUM formula to "A4" cell
worksheet.getCells().get("A4").setFormula("=SUM(A1:A3)");
// Calculating the results of formulas
workbook.calculateFormula();
// Get the calculated value of the cell
String value = worksheet.getCells().get("A4").getStringValue();
// Saving the Excel file
workbook.save(dataDir + "test.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CalculatingFormulas.class) + "formulas/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a value to "A1" cell
worksheet.getCells().get("A1").putValue(1);
// Adding a value to "A2" cell
worksheet.getCells().get("A2").putValue(2);
// Adding a value to "A3" cell
worksheet.getCells().get("A3").putValue(3);
// Adding a SUM formula to "A4" cell
worksheet.getCells().get("A4").setFormula("=SUM(A1:A3)");
// Calculating the results of formulas
workbook.calculateFormula();
// Get the calculated value of the cell
String value = worksheet.getCells().get("A4").getStringValue();
// Saving the Excel file
workbook.save(dataDir + "CalculatingFormulas_out.xls");
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(CalculatingFormulasOnce.class);
// Load the template workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Print the time before formula calculation
System.out.println(DateTime.getNow());
// Set the CreateCalcChain as false
workbook.getSettings().setCreateCalcChain(false);