Autosize only Merged Cells using Aspose - excel

I saw that there was a way to autofit rows in the worksheet. But I only want to autofit the rows that have only merged cells in it. And keep all of the other rows the same. Not sure if there is a way to do this.
I've tried this but it autofits all rows.
AutoFitterOptions options = new AutoFitterOptions();
options.AutoFitMergedCells = true;
_worksheet.AutoFitRows(options);
And I won't know the exact row that needs to be autofitted because I'm adding data to the excel sheet.

Please try the following sample code for your needs:
e.g.
Sample code:
Workbook wb = new Workbook("e:\\test2\\Book1.xlsx");
Worksheet _worksheet = wb.Worksheets[0];
var cells = _worksheet.Cells;
foreach (Cell cell in cells)
{
if (cell.IsMerged)
{
int row = cell.Row;
AutoFitterOptions options = new AutoFitterOptions();
options.AutoFitMergedCells = true;
_worksheet.AutoFitRows(row, row, options);
}
}
wb.Save("e:\\test2\\out1.xlsx");
Hope, this helps a bit.
You may also post your queries in dedicated forums.
PS. I am working as Support developer/ Evangelist at Aspose.

Related

How to update the source data range for existing chart

I'm trying to write a script that finds data in a sheet dynamically (the dimensions of the table need to be flexible in both axis) and then updates the source data range for an existing chart on another sheet (so that my users don't need to set up the styling themselves).
Below is my script so far. Everything works apart from the final line where Excel Online gives me the error:
"Line 10: Chart setData: You cannot perform the requested operation"
function main(workbook: ExcelScript.Workbook)
{
let selectedSheet = workbook.getWorksheet("Enter data in this sheet");
// Add a new table at used range on selectedSheet
let range = selectedSheet.getUsedRange();
if(selectedSheet.getTables().length == 0)
{
let newTable = workbook.addTable(range, true);
}
workbook.getWorksheet("The Chart").getChart("Chart 1").setData(range);
}
The worksheet names are correct and so is the chart name as far as I can see:
screenshot of excel online showing chart and worksheet names
Answer found; the chart was on a protected sheet which did not allow editing of objects. Updating the protection to allow all users to edit objects has resolved the issue.

How to edit specific excel sheet

I have a maser sheet containing multiple sheets. I want to editing DATA sheet shown in the image and I can edit but when i edit the DATA sheet then the data and pivot tables and styling and formatting in Main repot and pivot sheets gets blank . how to stop being formatting and styling gets blank.
I am using laravel with maatwebiste.
below is the originalsheet
but when i store and download the updated sheet the sheet gets blank
this is data sheet
Excel::selectSheetsByIndex(2)->load($final_file_path, function($reader) {
$reader->sheet('Data', function($sheet) {
$select_arrays = ['85213','Age','40-49','2019-12-01','111111','Not Stated','Not Stated'];
$sheet->appendRow($select_arrays);
}, 'UTF-8')->store('xlsx', storage_path('/'), true);
Above code is working fine but when store to the path all formatting pivots are removed
Earlier I was using maatwebiste that does not support Pivot but now in my solution I used COM library used for excel.
Converting excel to pdf using PHP
The solution for the above problem mentioned below:-
// WORKBOOK AND WORKSHEET OBJECTS
$wbk = $xlapp->Workbooks->Open("C:\\xampp\htdocs\php-excel1\PivotReportMockUp.xlsx");
$wks = $wbk->Worksheets(2);
$wks1 = $wbk->Worksheets(1);
// SET CELL VALUE
$wks->Range("B2")->Value = "85552";
$wbk->save();
// OUTPUT WORKSHEET TO PDF
$xlTypePDF = 0;
$xlQualityStandard = 0;
$OpenAfterPublish= True; //`enter code here`
$IgnorePrintAreas = True;
try {
$wks1->ExportAsFixedFormat($xlTypePDF, "C:\\xampp\htdocs\php-excel1\PivotReport.pdf", $xlQualityStandard,$OpenAfterPublish,$IgnorePrintAreas);
} catch(com_exception $e) {
echo $e->getMessage()."\n";
exit;
}

openxml sdk excel total sums

i am having difficulties with open xml sdk:
i am trying to generate excel file with several columns that have numbers and i want to have total sum at the end
i have tried to Generate Table Definition Part Content and inside define every column (id, name etC). If column has true for TotalColumn, it adds code (rough example)
var column = new TableColumn{
id = 1,
name = "example",
TotalsRowFunction = TotalsRowFunctionValues.Sum,
TotalsRowFormula = new TotalsRowFormula("=SUBTOTAL(109;[" + rowName + "])")
};
I can't get it to work, when i open excel it reports error, but it doesn't explicitly says what the problem is... I tried with microsoft validator but can't figure anything out...
I'd appreciate any help / example code since i can't google anything out
EDIT:
i use this at the end:
workbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
workbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
Why not use a cell formula?
E.g.
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
cell.CellFormula = new CellFormula(string.Format("=SUBTOTAL({0};[{1}])", "109", rowName));
//This will force a full recalculation of all the cells
workbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
workbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
I ended using EPPlus for this as it seems to be working simple and efficient

Creating an Excel Table with MATLAB

Since writing varying sized data ranges to a sheet seems to remove an Excel Table if the data range is larger than the existing Excel tables range, I want to create a Table in Excel every time I run the code. I'm currently having a fair bit of difficulty creating the tables. The code I have right now to try and create the ListObject:
eSheets = e.ActiveWorkbook.Sheets;
eSheet = eSheets.get('Item', j);
eSheet.Activate;
eSheet.Range(horzcat('A1:R',mat2str(size(obj,1)+1))).Select;
eSheet.Listobjects.Add;
eSheet.Listobjects.Item(1).TableStyle = 'TableStyleMedium2';
eSheet.ListObjects.Item(1).Name = tablename;
Any commentary or suggestions would be appreciated
I dont know about using eSheet in matlab but with the function
xlswrite(filename,A,sheet,xlRange)
you can also write your data from a matrix to an excel table http://de.mathworks.com/help/matlab/ref/xlswrite.html and with
[A,B] = xlsfinfo('foofoo.xlsx');
sheetValid = any(strcmp(B, 'foo2'));
you can also check if a table sheet already exist so that you wont override the old one and create a new one, as seen in https://de.mathworks.com/matlabcentral/answers/25848-how-to-check-existence-of-worksheet-in-excel-file
I am not sure if this is what you are looking for thougth
Alright, since the post got downvoted (not sure why...) I found my own answer with the help of some VBA forums and MATLAB Newsgroup. Here's what the final code looks like for anyone else that has issues:
e = actxserver('Excel.Application');
ewb = e.Workbooks.Open('Path/to/file');
eSheets = e.ActiveWorkbook.Sheets;
eSheet = eSheets.get('Item', j);
eSheet.Activate;
range = horzcat('A1:R',mat2str(size(obj,1)+1));
range_todelete = horzcat('A1:R',mat2str(size(obj,1)+300));
Range1 = eSheet.get('Range',range_todelete);
Range1.Value=[];
eSheet.Range(range).Select;
name = 'Table_Name';
try eSheet.ListObjects(name).Item(1).Delete
catch
end
eSheet.Listobjects.Add;
eSheet.ListObjects.Item(1).Name = name;
eSheet.ListObjects.Item(1).TableStyle = 'TableStyleMedium2';
Range = eSheet.get('Range',range);
Range.Value = cellarray;

Hidden Cells still showing in an OfficeWriter Excel Spreadsheet

When my Office Writer Excel report opens, it randomly un-hides some of the hidden cells and columns. I have verified that it is not the data that causes the columns or cells to not be hidden. Has anyone experienced this before and is there a way to make sure that all columns or cells stay hidden when the excel file is opened?
I work for SoftArtisans. We have not had any other reports of programmatically hidden columns becoming visible in the output file. We also have not been able to reproduce the behavior you are reporting. It would be helpful to see a code snippet, as well as to know which version of OfficeWriter you are using and which version of Excel is being used to open the output file.
There are two ways to hide columns with our API, both using the ColumnProperties object. You can set the hidden property to true or set the width property to zero. You could do both if you like, although that shouldn't be necessary.
For example:
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create(ExcelApplication.FileFormat.Xlsx);
//or if opening an existing workbook
//Workbook wb = xla.Open(inputFilePath);
//Get a handle on the worksheet
Worksheet ws = wb.Worksheets[0];
//Write a value to a cell
ws.Cells[0, 9].Value = "Hidden Value";
//Get a handle on the column you want to hide
ColumnProperties colProps = ws.GetColumnProperties(9);
//set the column to hidden
colProps.Hidden = true;
//or set the column width to zero
colProps.Width = 0;
//Stream the output file to the response
xla.Save(wb, Page.Response, "HiddenColumnTest.xlsx", false);

Resources