tab 1
此 demo 展示如何创建 excel 图表。
如果这不是您想要的 demo,您可以通过填写表格获取免费定制 demo。
如您有与我们产品相关的其他技术问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。;销售相关的问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。。
tab 2
using system.data;
using spire.xls;
namespace demoonlinecode
{
class charts
{
public void demochart(datatable table, excelcharttype chartformat, string resultfilename)
{
workbook book = new workbook();
worksheet sheet = book.worksheets[0];
sheet.insertdatatable(table, true, 1, 1);
setchart(sheet, chartformat);
sheetstyle(book, sheet);
book.savetofile(resultfilename,excelversion.version2010);
}
private void setchart(worksheet sheet, excelcharttype chartformat)
{
sheet.name = "chart data";
sheet.gridlinesvisible = false;
//writes chart data
//createchartdata(sheet);
//add a new chart worsheet to workbook
chart chart = sheet.charts.add();
//set region of chart data
chart.datarange = sheet.range["a1:c7"];
chart.seriesdatafromrange = false;
//set position of chart
chart.leftcolumn = 1;
chart.toprow = 8;
chart.rightcolumn = 11;
chart.bottomrow = 29;
chart.charttype = chartformat;
//chart title
chart.charttitle = "sales market by country";
chart.charttitlearea.isbold = true;
chart.charttitlearea.size = 12;
chart.primarycategoryaxis.title = "country";
chart.primarycategoryaxis.font.isbold = true;
chart.primarycategoryaxis.titlearea.isbold = true;
chart.primaryvalueaxis.title = "sales(in dollars)";
chart.primaryvalueaxis.hasmajorgridlines = false;
chart.primaryvalueaxis.titlearea.textrotationangle = 90;
chart.primaryvalueaxis.minvalue = 1000;
chart.primaryvalueaxis.titlearea.isbold = true;
foreach (spire.xls.charts.chartserie cs in chart.series)
{
cs.format.options.isvarycolor = true;
cs.datapoints.defaultdatapoint.datalabels.hasvalue = true;
}
chart.legend.position = legendpositiontype.top;
}
public static void sheetstyle(workbook book, worksheet sheet)
{
cellstyle oddstyle = book.styles.add("oddstyle");
oddstyle.borders[borderslinetype.edgeleft].linestyle = linestyletype.thin;
oddstyle.borders[borderslinetype.edgeright].linestyle = linestyletype.thin;
oddstyle.borders[borderslinetype.edgetop].linestyle = linestyletype.thin;
oddstyle.borders[borderslinetype.edgebottom].linestyle = linestyletype.thin;
oddstyle.knowncolor = excelcolors.lightgreen1;
cellstyle evenstyle = book.styles.add("evenstyle");
evenstyle.borders[borderslinetype.edgeleft].linestyle = linestyletype.thin;
evenstyle.borders[borderslinetype.edgeright].linestyle = linestyletype.thin;
evenstyle.borders[borderslinetype.edgetop].linestyle = linestyletype.thin;
evenstyle.borders[borderslinetype.edgebottom].linestyle = linestyletype.thin;
evenstyle.knowncolor = excelcolors.lightturquoise;
foreach (cellrange range in sheet.allocatedrange.rows)
{
if (range.row % 2 == 0)
range.cellstylename = evenstyle.name;
else
range.cellstylename = oddstyle.name;
}
//sets header style
cellstyle styleheader = sheet.rows[0].style;
styleheader.borders[borderslinetype.edgeleft].linestyle = linestyletype.thin;
styleheader.borders[borderslinetype.edgeright].linestyle = linestyletype.thin;
styleheader.borders[borderslinetype.edgetop].linestyle = linestyletype.thin;
styleheader.borders[borderslinetype.edgebottom].linestyle = linestyletype.thin;
styleheader.verticalalignment = verticalaligntype.center;
styleheader.knowncolor = excelcolors.green;
styleheader.font.knowncolor = excelcolors.white;
styleheader.font.isbold = true;
sheet.columns[sheet.allocatedrange.lastcolumn - 1].style.numberformat = "\"$\"#,##0";
sheet.columns[sheet.allocatedrange.lastcolumn - 2].style.numberformat = "\"$\"#,##0";
sheet.allocatedrange.autofitcolumns();
sheet.allocatedrange.autofitrows();
sheet.rows[0].rowheight = 20;
}
}
}
tab 3
imports system.data
imports spire.xls
namespace demoonlinecode
class charts
public sub demochart(table as datatable, chartformat as excelcharttype, resultfilename as string)
dim book as new workbook()
dim sheet as worksheet = book.worksheets(0)
sheet.insertdatatable(table, true, 1, 1)
setchart(sheet, chartformat)
sheetstyle(book, sheet)
book.savetofile(resultfilename, excelversion.version2010)
end sub
private sub setchart(sheet as worksheet, chartformat as excelcharttype)
sheet.name = "chart data"
sheet.gridlinesvisible = false
'writes chart data
'createchartdata(sheet);
'add a new chart worsheet to workbook
dim chart as chart = sheet.charts.add()
'set region of chart data
chart.datarange = sheet.range("a1:c7")
chart.seriesdatafromrange = false
'set position of chart
chart.leftcolumn = 1
chart.toprow = 8
chart.rightcolumn = 11
chart.bottomrow = 29
chart.charttype = chartformat
'chart title
chart.charttitle = "sales market by country"
chart.charttitlearea.isbold = true
chart.charttitlearea.size = 12
chart.primarycategoryaxis.title = "country"
chart.primarycategoryaxis.font.isbold = true
chart.primarycategoryaxis.titlearea.isbold = true
chart.primaryvalueaxis.title = "sales(in dollars)"
chart.primaryvalueaxis.hasmajorgridlines = false
chart.primaryvalueaxis.titlearea.textrotationangle = 90
chart.primaryvalueaxis.minvalue = 1000
chart.primaryvalueaxis.titlearea.isbold = true
for each cs as spire.xls.charts.chartserie in chart.series
cs.format.options.isvarycolor = true
cs.datapoints.defaultdatapoint.datalabels.hasvalue = true
next
chart.legend.position = legendpositiontype.top
end sub
public shared sub sheetstyle(book as workbook, sheet as worksheet)
dim oddstyle as cellstyle = book.styles.add("oddstyle")
oddstyle.borders(borderslinetype.edgeleft).linestyle = linestyletype.thin
oddstyle.borders(borderslinetype.edgeright).linestyle = linestyletype.thin
oddstyle.borders(borderslinetype.edgetop).linestyle = linestyletype.thin
oddstyle.borders(borderslinetype.edgebottom).linestyle = linestyletype.thin
oddstyle.knowncolor = excelcolors.lightgreen1
dim evenstyle as cellstyle = book.styles.add("evenstyle")
evenstyle.borders(borderslinetype.edgeleft).linestyle = linestyletype.thin
evenstyle.borders(borderslinetype.edgeright).linestyle = linestyletype.thin
evenstyle.borders(borderslinetype.edgetop).linestyle = linestyletype.thin
evenstyle.borders(borderslinetype.edgebottom).linestyle = linestyletype.thin
evenstyle.knowncolor = excelcolors.lightturquoise
for each range as cellrange in sheet.allocatedrange.rows
if range.row mod 2 = 0 then
range.cellstylename = evenstyle.name
else
range.cellstylename = oddstyle.name
end if
next
'sets header style
dim styleheader as cellstyle = sheet.rows(0).style
styleheader.borders(borderslinetype.edgeleft).linestyle = linestyletype.thin
styleheader.borders(borderslinetype.edgeright).linestyle = linestyletype.thin
styleheader.borders(borderslinetype.edgetop).linestyle = linestyletype.thin
styleheader.borders(borderslinetype.edgebottom).linestyle = linestyletype.thin
styleheader.verticalalignment = verticalaligntype.center
styleheader.knowncolor = excelcolors.green
styleheader.font.knowncolor = excelcolors.white
styleheader.font.isbold = true
sheet.columns(sheet.allocatedrange.lastcolumn - 1).style.numberformat = """$""#,##0"
sheet.columns(sheet.allocatedrange.lastcolumn - 2).style.numberformat = """$""#,##0"
sheet.allocatedrange.autofitcolumns()
sheet.allocatedrange.autofitrows()
sheet.rows(0).rowheight = 20
end sub
end class
end namespace