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.
Related
I need to be able to convert all charts in an excel spreadsheet to images programmatically (via c#),
I've tried Aspose.cells and it works perfectly, but i need a free alternative.
EPplus and other non-commercial libraries i know do not have the ability to do this,
and i'm in an web environment so I cannot use Office Interop.
My question is: is there any way I can programmatically read an .xlsx file and swap every chart it contains for an image of said chart, without depending on a paid library ?
Try Spire.Xls
var wb = new Spire.Xls.Workbook();
var sheet=wb.Worksheets[0];
//Replace charts with png
var charts = summarySheet.Charts.Select(x => (Chart)x).ToArray();
if (charts.Any())
{
var imgs = wb.SaveChartAsImage(summarySheet); //same indexes as chart obj arr
for (int i = 0; i <= charts.Length - 1; i++)
{
var chart = charts[i];
var chartImg = imgs[i];
summarySheet.Pictures.Add(chart.TopRow, chart.LeftColumn, chartImg, 100, 100, ImageFormatType.Png);
}
}
Have you tried ClosedXML.Excel in nuget?
XLSX support
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
worksheet.Cell("A2").FormulaA1 = "=MID(A1, 7, 5)";
workbook.SaveAs("HelloWorld.xlsx");
}
source https://github.com/ClosedXML/ClosedXML
I want to create an excel worksheet with an organizational chart with data from a database.
I have played around with the interop excel library in a c# console application, but i am stuck.
I can make a worksheet containing a smartart object with the hierachy chart layout(Org chart), but i dont know how to add my own data to it. When you create the chart it comes with the nodes shown in the picture below.
Is it even possible to add you own data to the chart through the interop excel library?
This is my code so far:
private static Excel.Workbook Wb = null;
private static Excel.Application Xl = null;
private static Excel.Worksheet Sheet = null;
static void Main(string[] args)
{
Xl = new Excel.Application();
Xl.Visible = true;
Wb = Xl.Workbooks.Add();
Sheet = Wb.Worksheets[1];
var myLayout = Xl.SmartArtLayouts[88];
var smartArtShape = Sheet.Shapes.AddSmartArt(myLayout, 50, 50, 200, 200);
smartArtShape.AlternativeText = "Test";
}
This is the result:
Yes, the functionality is accessible through the "interop" :-) It's just a bit... arcane, especially for those who don't come from PowerPoint.
Since a SmartArt is "wrapped" in a Shape object, it's first necessary to get the Shape, then through it the SmartArt - the Shape object has HasSmartArt (test) and SmartArt (use) properties.
(Note that I find it very useful to explicitly declare the object type, rather than rely on var to "guess". That helps immensely when trouble-shooting.)
The actual content (the "boxes") are Nodes. For some reason, the PIAs don't provide Node objects (but they are present in the COM object model), so accessing them is a bit round-about in C#. To get all the nodes, use the AllNodes property to return the collection.
The Node objects can then be addressed collection indexing nds[i]. An individual node can be assigned to a SmartArtNode or to an Office Shape object. For the purposes of this question - adding text - the next step is the same...
The "boxes" are, themselves, Office Drawing Text Boxes (Shapes) and have the TextFrame2.TextRange.Text property for accessing their content. (Note that the default content is actually a placeholder, so this property will not return anything for a newly created SmartArt.)
Note: The code in the question attempts to use AlternativeText - this is like "AltText" in HTML objects, it provides accessibilty information and is not relevant for what you're trying to do.
static void Main(string[] args)
{
excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook wb = excelApp.Workbooks.Add();
Excel.Worksheet ws = (Excel.Worksheet) wb.Worksheets[1];
var myLayout = excelApp.SmartArtLayouts[88];
var smartArtShape = ws.Shapes.AddSmartArt(myLayout, 50, 50, 200, 200);
if (smartArtShape.HasSmartArt == Office.MsoTriState.msoTrue)
{
Office.SmartArt smartArt = smartArtShape.SmartArt;
Office.SmartArtNodes nds = smartArt.AllNodes;
for (int i = 1; i <= nds.Count; i++)
{
Office.SmartArtNode nd = nds[i]; //both work
var shpNode = nd.Shapes.Item(1);
nd.TextFrame2.TextRange.Text = "Testing Node " + i;
shpNode.TextFrame2.TextRange.Text = "Testing Shape " + i;
}
}
}
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 :
Here is an excel file with one worksheet in it,also with some data in the worksheet.
Then the user create a chart,just like 'xlLine' or 'xlColumnClustered',and set some config info such as chart title,CategoryTitle,ValueTitle and so on.
And how can i get the config info,especially the selected data area of the chart?
Ths!
the worksheet and chart
some code
//Get the current worksheet
Microsoft.Office.Tools.Excel.Worksheet worksheet = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet);
//Get ChartObjects
Microsoft.Office.Interop.Excel.ChartObjects objs = (ChartObjects)worksheet.ChartObjects(Type.Missing);
//Just get the first ChartObject
Microsoft.Office.Interop.Excel.ChartObject obj = objs.Item(1);
//Get the chartTitle
string chartTitle = obj.Chart.ChartTitle.Caption;
I am trying to use Spire dll to convert excel file to jpg .
so i have a xlsm file like this :
i use this code to convert it to jpg :
Workbook workbook = new Workbook();
workbook.LoadFromFile(#"D:\a.xlsm");
Worksheet sheet = workbook.Worksheets[1];
sheet.SaveToImage("exceltoimage.jpg");
System.Diagnostics.Process.Start("exceltoimage.jpg");
but the output is like this some of the cell can't be converted why i mean (#name)?
I have this warning in my page :
I finally use Aspose :
http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/category1129.aspx
example :
http://www.aspose.com/docs/display/cellsnet/Converting+Worksheet+to+Image
my sample code :
Workbook workbook = new Workbook(#"D:\a.xlsm");
//Get the first worksheet.
Worksheet sheet = workbook.Worksheets[12];
//Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
//Specify the image format
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
//Only one page for the whole sheet would be rendered
imgOptions.OnePagePerSheet = true;
//Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, imgOptions);
//Render the image for the sheet
Bitmap bitmap = sr.ToImage(0);
//Save the image file specifying its image format.
bitmap.Save(#"d:\SheetImage.jpg");
It works great for me .