Hi folks,
I have generated an Excel using Open XML and input some data. Now I want to create a Bar Chart based on these data; however I have no idea how to continue. Can anyone share a code sample for that? Thanks very much in advance.
Below is the codes I used(this part without any error):
using System;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Drawing.Charts;
namespace openxmlExcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create("D:\\test.xls", SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
string relld = workbookpart.GetIdOfPart(worksheetPart);
Workbook workbook = new Workbook();
FileVersion fileVersion = new FileVersion { ApplicationName = "MS Excel" };
Worksheet worksheet = new Worksheet();
SheetData sheetData = new SheetData();
Row contentRow1 = CreateTextRow(4,"Market1","Market2","Market3");
sheetData.AppendChild(contentRow1);
Row contentRow2 = CreateContentRow(5,"2009",120,90,110);
sheetData.AppendChild(contentRow2);
Row contentRow3 = CreateContentRow(6,"2010",152,69,100);
sheetData.AppendChild(contentRow3);
Row contentRow4 = CreateContentRow(7,"2011",163,65,54);
sheetData.AppendChild(contentRow4);
Row contentRow5 = CreateContentRow(8,"2012",120,35,56);
sheetData.AppendChild(contentRow5);
worksheet.Append(sheetData);
worksheetPart.Worksheet = worksheet;
worksheetPart.Worksheet.Save();
Sheets sheets=new Sheets();
Sheet sheet = new Sheet {Name="Sheet-chart",SheetId=1,Id=relld};
sheets.Append(sheet);
workbook.Append(fileVersion);
workbook.Append(sheets);
spreadsheetDocument.WorkbookPart.Workbook = workbook;
spreadsheetDocument.WorkbookPart.Workbook.Save();
spreadsheetDocument.Close();
System.Diagnostics.Process.Start("D:\\test.xls");
}
}
private OpenXmlElement[] Formula(SheetData sheetData)
{
throw new NotImplementedException();
}
private static Row CreateTextRow(UInt32 index,string header1, string header2,string header3)
{
Row r = new Row { RowIndex =index };
Cell cell1 = CreateTextCell("D", index, header1);
Cell cell2 = CreateTextCell("E", index, header2);
Cell cell3 = CreateTextCell("F", index, header3);
r.Append(cell1);
r.Append(cell2);
r.Append(cell3);
return r;
}
private static Row CreateContentRow(UInt32 index,string years,int sale1,int sale2,int sale3)
{
Row r=new Row{RowIndex=index};
Cell cell1 = CreateTextCell( "C", index,years);
Cell cell2 = CreateNumberCell("D", index, sale1);
Cell cell3 = CreateNumberCell("E",index,sale2);
Cell cell4 = CreateNumberCell("F",index,sale3);
r.Append(cell1);
r.Append(cell2);
r.Append(cell3);
r.Append(cell4);
return r;
}
private static Cell CreateTextCell(string header, UInt32 index, string text)
{
Cell c = new Cell { DataType = CellValues.InlineString,CellReference=header+index };
InlineString istring = new InlineString();
Text t = new Text { Text = text };
istring.Append(t);
c.Append(istring);
return c;
}
private static Cell CreateNumberCell(string header, UInt32 index, int number)
{
Cell c = new Cell {CellReference=header+index};
CellValue v = new CellValue { Text = number.ToString() };
c.Append(v);
return c;
}
}
}
Please Help...
Best Regards
Gerxy