Formatting not retain in closedXML - c#

I am creating a XLSX file using XLWorkbook (ClosedXML.Excel).
I want to format the columns to percentage/decimal/number etc.
I have used the following
oSheet.Range("CL2:CL200").Style.NumberFormat.SetFormat("0.00%");
when I checked the excel its still showing the result as 0.2030 i.e without percentage. But when I double click the column format change to percentage (20.30%).
Why column not showing percentage when I first open the excel i.e. without double click.
How can I retain the format after creating excel in c# code
Code sample is :
DataTable oDataTable = GetDataTable(oData);
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(oDataTable, "Sheet1");
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
wb.Style.Font.Bold = true;
IXLWorksheet oSheet;
wb.TryGetWorksheet("Sheet1", out oSheet);
ange("B1:B200").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
oSheet.Range("C1:C200").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
oSheet.Range("D1:D200").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
oSheet.Range("CL2:CL200").Style.NumberFormat.SetFormat("0.00%");
oSheet.Column("CL").AdjustToContents();
wb.SaveAs(fullPath);
}
After double clicking I an getting the correct format as :

Related

Syncfunsio xslio exports null instead of blank in excel in C#

I am using Syncfusion library to generate an excel report. I referred syncfusion to generate the report using template makers. The data is correctly exported in excel file but it is exporting NULL instead of blank string for some of nullable number columns. How can I write blank instead of NULL in excel for nullable fields.
Template file:
Output:
This is the code I am using.
using Syncfusion.XlsIO;
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2010;
IWorkbook workbook = application.Workbooks.Open(_templateFilePath);
IWorksheet worksheet = workbook.Worksheets[0];
ITemplateMarkersProcessor marker = workbook.CreateTemplateMarkersProcessor();
var data = _database.GetData();
marker.AddVariable("DATA", data);
marker.ApplyMarkers();
workbook.SaveAs(_stReportFilePath);
workbook.Close();
excelEngine.Dispose();
}

Script C# in SSIS to manipulate an excel sheet

I'm trying to create an script C# in SSIS to create a new column in a sheet on Excel.
I need to know the IndentLevel of a cell in excel and for this i have to create a new column with this values.
I'm trying to do this (Script in c#):
Range values = sheet.get_Range("A13");
values.Value = sheet.Range["B13"].IndentLevel();
In VBA Works like this (Script in VBA inside of a excell):
Range("A16").Value = Range("B16").IndentLevel
In C# how can i do that? i'm trying everything but doenst work.
Complete script:
xlApp = new _Excel.Application();
xlApp.Visible = true;
oWB = (_Excel.Workbook)xlApp.Workbooks.Open(destFile);
_Excel.Worksheet sheet = (_Excel.Worksheet)xlApp.Worksheets[1];
sheet.Columns["B:N"].Delete();
Range values = sheet.get_Range("A13");
values.Value = sheet.Range["B13"].IndentLevel();
Getting rid of the parenthesis seems to do the task correctly.
string destFile = #"E:\StackOverflow\Sample.xlsx";
var xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = true;
var oWB = (Microsoft.Office.Interop.Excel.Workbook)xlApp.Workbooks.Open(destFile);
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)xlApp.Worksheets[1];
sheet.Range["A16"].Value = sheet.Range["B16"].IndentLevel;
The value in cell A16 is set to B16's indent level.
The only other note is to make sure that the file isn't open elsewhere, otherwise the code will open up a read-only copy.

Trying to create an excel chart in a XLS file with data

My problem currently is that the data shows up like it's suppose to in the excel file but the chart doesn't appear correctly. I am trying Method 1: http://www.dotnetperls.com/excel.
I have already looked at this: Excel Interop Apply Chart Template
Charts: (http://imgur.com/a/btgWT)
The top image is what I get the bottom image is what I want.
string tempPath = Path.GetTempPath();
//C:\appdata...\LicenseCheck
string folderPath = string.Format(#"{0}\LicenseCheck", tempPath);
//The csv file with the XMAX license usage stats
string secondPath = string.Format(#"{0}\{1}_Report_Stats_{2}_{3}.csv",
folderPath, productName, lastCheckedDate.Month, lastCheckedDate.Year);
var application = new Application();
var workbook = application.Workbooks.Open(secondPath);
var worksheet = workbook.Worksheets[1] as Worksheet;
object misValue = System.Reflection.Missing.Value;
// Add chart.
var charts = worksheet.ChartObjects() as ChartObjects;
var chartObject = charts.Add(60, 10, 300, 300) as ChartObject;
var chart = chartObject.Chart;
// Set chart range.
var range = worksheet.get_Range("A1", "C25");
chart.SetSourceData(range);
// Set chart properties.
chart.ChartType = XlChartType.xlColumnClustered;
chart.ChartWizard
(
Source: range
// Title: "graphTitle",
// CategoryTitle: "xAxis",
// ValueTitle: "yAxis"
);
chartObject.Chart.ApplyChartTemplate(#"C:\\LicenseUsageStatsTemplate.crtx");
// Save.
workbook.SaveAs("Stats.xls", XlFileFormat.xlWorkbookNormal);
workbook.Close(true, misValue, misValue);
application.Quit();
releaseObject(worksheet);
releaseObject(workbook);
releaseObject(application);
Found out that MS office had to be installed on the server to use Interop. So, ended up using EPPlus. It's an awesome library that uses OfficeOpenXML.

How to apply strike formatting using EPPlus

I'm using EPPlus in a web application with C#. I need to read an Excel file and check its format, I tried it doing the same as this article (How do I get partial cell styling in excel using EPpplus?), and actually all format properties were ok (bold, italic, color...), but the one that I really need is to check the strike
text property and it is always set to false.
Here is an answer just so the question doesnt hang out there:
[TestMethod]
public void Strike_Format_Test()
{
//http://stackoverflow.com/questions/30517646/how-to-apply-strike-formatting-using-epplus
var existingFile = new FileInfo(#"c:\temp\StrikeFormat.xlsx");
using (var pck = new ExcelPackage(existingFile))
{
var wb = pck.Workbook;
var ws = wb.Worksheets.First();
var cell = ws.Cells["A1"];
Console.WriteLine(cell.Style.Font.Strike);
}
}

Remember hidden rows after data refresh in Excel c#

I am trying to do the following using c# code:
Hide some rows in excel.
Clear all data and formats in excel sheet.
Put other data to excel sheet.
I would like the hidden rows still remain hidden.
Is it possible?
Thank You!
I've had great results using ClosedXML to manipulate excel spreadsheets.
While I haven't tried your case I've done similar things. In my case I put my private data into a new worksheet and hide that, which ClodedXML made simple.
Here's a sample code that can get you going....
//Create an Excel App
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook xlWorkBook = null;
Microsoft.Office.Interop.Excel._Worksheet xlWorksheet;
//Open a Workbook
xlWorkBook = xlApp.Workbooks.Open(#"d:\test.xlsx");
xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets[1];
//My Workbook contains 10 rows with some data and formatting
//I Hide rows 3, 4 & 5
Microsoft.Office.Interop.Excel.Range hiddenRange = xlWorksheet.get_Range("A3:C5");
hiddenRange.EntireRow.Hidden = true;
//Get the entire sheet and Clear everything on it including data & formatting
Microsoft.Office.Interop.Excel.Range allRange = xlWorksheet.UsedRange;
allRange.Clear();
//Now Add some new data, say a Title on the first cell, and some more data in a loop later
xlWorksheet.Cells[1, 1] = "Title";
for (int i = 6; i < 10; i++)
{
xlWorksheet.Cells[i, 1] = i.ToString();
}
xlApp.Visible = true;
Thats it....
Store them in a variable and hide them again after you have populated excel with data.

Categories

Resources