I'm working with Spire.XLS licenced pack and I need to find a way to set the values for a chart manually from a c# win form application, speaking about category labels to actual values to display inside, instead of being forced to use sheet.Range[], or at least be able to use single cell values separated one from another from a sheet and use those like an "extended range" for the values any idea if this is possible?
Yes, it is possible. See the following example which shows how to create a chart without using worksheet data range.
using Spire.Xls;
namespace Create_chart
{
class Program
{
static void Main(string[] args)
{
//Create a workbook
Workbook wb = new Workbook();
//Get the first worksheet
Worksheet sheet = wb.Worksheets[0];
//Add a chart to the worksheet
Chart chart = sheet.Charts.Add();
//Add a series to the chart
var series = chart.Series.Add();
//Add data
series.EnteredDirectlyValues = new object[] { 10, 20, 30 };
//Save the file
wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);
}
}
}
For more information, you can check this documentation: Create Chart without Using Worksheet Data Range in C#
Related
I am creating charts in PowerPoint. The below code opens two excel applications. One opens in the background that is invisible. The second one opens after the method ends. I need to make sure second excel either never open ideally or I can close it after it opens.
I have tried the below things but none worked.
I have tried forcing GC, Manual ReleaseComObject, Killing Excel process
I have tried separating excel COM objects and forcing GC
private void BtnInsert_Click(object sender, EventArgs e)
{
var Addin = Globals.ThisAddIn;
Microsoft.Office.Interop.PowerPoint.Application activeApplication = Addin.Application;
DocumentWindow activeWindows = activeApplication.ActiveWindow;
Microsoft.Office.Interop.PowerPoint.View activeView = activeWindows.View;
Slide activeSlide = activeView.Slide;
Microsoft.Office.Interop.PowerPoint.Shapes slideShape = activeSlide.Shapes;
Microsoft.Office.Interop.PowerPoint.Shape shape = slideShape.AddChart2(-1, XlChartType.xl3DBarClustered, -1, -1, -1, -1, true);
Microsoft.Office.Interop.PowerPoint.Chart chart = shape.Chart;
//Access the chart data
Microsoft.Office.Interop.PowerPoint.ChartData chartData = chart.ChartData;
chartData.Activate();
//Create instance to Excel workbook to work with chart data
Workbook workbook = chartData.Workbook;
Microsoft.Office.Interop.Excel.Application workbookApplication = workbook.Application;
workbookApplication.Visible = false;
workbookApplication.WindowState = XlWindowState.xlMinimized;
//Accessing the data worksheet for chart
Worksheet worksheet = workbook.Worksheets[1];
// I am adding data here
// This is not required to reproduce this
chartData.BreakLink();
workbook.Close(true);
}
Also, note that this issue does not occur while updating data.
Remove chartData.Activate() and chartData.BreakLink() solves this.
Although online documentation says that chartdata.activate is required before accessing the workbook.
Otherwise, we will get a null reference.
I think the documentation is incorrect or it does not apply to vsto.
This image show what i basically want to do
I have plenty excel files that i need to prepare before inserting the data into a SQL database, one of the steps is unmerge excel cells and duplicate the data, i'm doing this doc parse with c#
I found a solution with VBA Macro Excel here
Sub UnMergeFill()
Dim cell As Range, joinedCells As Range
For Each cell In ThisWorkbook.ActiveSheet.UsedRange
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
joinedCells.Value = cell.Value
End If
Next
End Sub
But i need to do it on c# with microsoft.office.interop.excel
Does anyone know if there's a way to do this?
C# code is very similar:
private void UnMergeFill(Workbook wb)
{
foreach (Range cell in ((_Worksheet)wb.ActiveSheet).UsedRange)
{
if (cell.MergeCells)
{
var joinedCells = cell.MergeArea;
cell.MergeCells = false;
joinedCells.Value = cell.Value;
}
}
}
Using EPPlus, how can i change a cells text to display as a vertical text like this,
In excel, you can do this by clicking on this button when setting cell orientation,
I'm trying to play around with the .TextRotation but this does not achieve what I want, setting it to something like 180 degrees will give me something like this,
ws.Cells[row, 2].Style.TextRotation = 180;, .TextRotation only accepts an integer value so I was wondering how I can get the "Text" buttons value,
Its definitely a bug you found. There is a way but it is pretty ugly. You can use the StyleID created by the cell when you change it to anything other than the default:
[TestMethod]
public void Text_Rotate_Test()
{
//https://stackoverflow.com/questions/57603348/display-cell-with-vertical-text-using-epplus
var fileInfo = new FileInfo(#"c:\temp\Text_Rotate_Test.xlsx");
if (fileInfo.Exists)
fileInfo.Delete();
using (var pck = new ExcelPackage(fileInfo))
{
var workbook = pck.Workbook;
var worksheet = workbook.Worksheets.Add("Sheet1");
var cell = worksheet.Cells[1, 1];
cell.Value = "Test Text Value";
//Trigger epplus to create a new style specific for the cell.
//This needs to be done even thought it will be overridden in
//order to ref by index. But have to be careful not to step
//on other styles so make it as unique as it needs to be.
cell.Style.TextRotation = 180;
//Make sure the update the xml before looking up by index
workbook.Styles.UpdateXml();
workbook.Styles.CellXfs[cell.StyleID].TextRotation = 255;
pck.Save();
}
}
Which gives this:
I want to add one picture (displaying "DRAFT") by printable Excel worksheet in C# EPPlus.
I need to know if there is a way to find the last visible row of each page of a worksheet when you are printing it. I can't pretend that it will always be a fix number of row per page because it depends on the content of the cells.
Here is my current code that use a fix number of row per page (30) to insert image. This result in approximately one image per printable page except that in each new page the image is not at the same place. (Slightly off, depending on content of cells.)
public void InsertDraftImage(ExcelWorksheet worksheet, FileInfo draft_image)
{
int maxRowNumber = worksheet.Dimension.End.Row;
int rowByPage = 30;
int numberOfPage = (maxRowNumber / rowByPage) + 1;
ExcelPicture picture = null;
for(int i = 0; i < numberOfPage; i++)
{
if(draft_image != null)
{
picture = worksheet.Drawings.AddPicture(i.ToString(), draft_image);
picture.SetSize(609, 545); //original image size
picture.SetPosition(i * rowByPage, 0, 1, 0);
picture.EditAs = eEditAs.Absolute;
}
}
After trying to implement the missing code in 'ExcelHeaderFooter.cs' from the EPPlus with a workmate without success, we finally did it by following Ernie suggestion!!
There is my final code to insert a picture into each page of a printable excel file generate with EPPlus in C#.
It is done by adding the picture in the footer and setting the Boolean ScaleWithDoc to false (default = true).
public void InsertDraftImage(ExcelWorksheet worksheet, FileInfo draft_image)
{
ExcelHeaderFooterText footer = worksheet.HeaderFooter.OddFooter; //all page have same footer
footer.InsertPicture(draft_image, PictureAlignment.Centered);
}
Added this code in my method to create the ExcelWorksheet (all the other excel style, populate, settings).
XmlAttribute temp = worksheet.WorksheetXml.CreateAttribute("scaleWithDoc");
temp.Value = "0";
worksheet.WorksheetXml.GetElementsByTagName("headerFooter")[0].Attributes.Append(temp);
package.Save();
Does anyone know how to set the excel page break to include a certain number of columns using C# with the OpenXML SDK? What I want to do is make x columns appear on one page. I had originally thought setting the print area would do it but it doesn't. I can't find any references to do this.
This is done manually in an excel spreadsheet's "Page Break View" where you drag the vertical dotted line to include more columns.
Thanks
The OpenXML SDK distinguish between manual horizontal page breaks and manual vertical page breaks.
A manual horizontal page break allows you to specify a break above a given row Id (index).
A vertical page break allows you to specify a break to the left of the specified column Id (index).
To programmatically insert a horizontal page break use the RowBreaks and Break class.
The RowBreaks class represents a collection of all horizontal page breaks in a worksheet.
The ColumnBreaks and Break class allow you to insert a vertical page break. The
ColumnBreaks class holds all vertical page breaks for a worksheet.
The following example demonstrate the insertion of a vertical page break.
The function InsertVerticalPageBreak() takes a columnIndex (where the page break should be inserted)
and the WorksheetPart. This function first checks if the worksheet already contains a
ColumnBreaks collection. If not, one will be created. Then the function creates an instance
of the Break class and sets the Id property to the column index. I've also set the Max property
to the maximum number of rows Excel is able to handle to get a continuing vertical page break. By setting the property ManualPageBreak to true we specifing a manual page break.
I've also added a InsertHorizontalPageBreak() function to the sample to show how to
add a horizontal page break.
private void InsertPageBreaks()
{
uint columnIndex = 17U;
uint rowIndex = 51U;
using (SpreadsheetDocument sd = SpreadsheetDocument.Open("c:\\temp\\spreadsheet.xlsx", true))
{
WorkbookPart workbookPart = sd.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.Last();
// Uncomment the following line to insert row page breaks.
// InsertHorizontalPageBreak(rowIndex, worksheetPart);
InsertColumnVerticalBreak(columnIndex, worksheetPart);
}
}
private void InsertHorizontalPageBreak(uint rowIndex, WorksheetPart worksheetPart)
{
Break rowBreak =
new Break() { Id = (UInt32Value)rowIndex, Max = (UInt32Value)16383U, ManualPageBreak = true };
RowBreaks rb = worksheetPart.Worksheet.GetFirstChild<RowBreaks>();
if (rb == null)
{
rb = new RowBreaks();
rb.ManualBreakCount = (UInt32Value)0;
rb.Count = (UInt32Value)0;
worksheetPart.Worksheet.Append(rb);
}
rb.Append(rowBreak);
rb.ManualBreakCount++;
rb.Count++;
}
private void InsertVerticalPageBreak(uint columnIndex, WorksheetPart worksheetPart)
{
ColumnBreaks cb = worksheetPart.Worksheet.GetFirstChild<ColumnBreaks>();
if (cb == null)
{
cb = new ColumnBreaks();
cb.ManualBreakCount = (UInt32Value)0;
cb.Count = (UInt32Value)0;
worksheetPart.Worksheet.Append(cb);
}
Break br =
new Break() { Id = (UInt32Value)columnIndex, Max = (UInt32Value)1048575U, ManualPageBreak = true };
cb.Append(br);
cb.ManualBreakCount++;
cb.Count++;
}