在以前接触的项目中,由于很多客户对微软Excel的操作比较熟练,客户经常要求系统支持对Excel文件的读写。用.NET传统方法对Excel进行读写时,往往会涉及到不同版本兼容的问题,导致在本地测试一切正常,但是到客户那经常会出现各种问题。后来发现一个Spire.XLS的NET库,可以很方便的对不同版本的Excel文件进度操作,还可以生成各种常见的图形。下面结合一个简单的例子,看一下效果:
1、首先要安装Spire.XLS,可以自定百度,然后建立一个windows应用程序WinExcelDemo,注意添加相关dll引用:
2、编写代码,这里当主界面打开时,就开始进行Excel的数据准备和文件生产操作,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WinExcelDemo
{
using Spire.Xls;
using Spire.Xls.Charts;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Run();
}
private void Run()
{
Workbook workbook = new Workbook(); //初始化 工作簿worksheet
workbook.CreateEmptySheets();
Worksheet sheet = workbook.Worksheets[];
sheet.Name = "数据";
sheet.GridLinesVisible = false; //创建图形数据
CreateChartData(sheet);
//在工作簿中添加一个新的图表
Chart chart = sheet.Charts.Add(); //设置数据的区域
chart.DataRange = sheet.Range["A1:C5"];
chart.SeriesDataFromRange = false; //设置图的位置
chart.LeftColumn = ;
chart.TopRow = ;
chart.RightColumn = ;
chart.BottomRow = ;
//图类型
chart.ChartType = ExcelChartType.Area;
//图标题
chart.ChartTitle = "Sales market by country";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = ; 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 = ;
chart.PrimaryValueAxis.MinValue = ;
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;
}
//Legend位置
chart.Legend.Position = LegendPositionType.Top;
workbook.SaveToFile("Sample.xls");
ExcelDocViewer(workbook.FileName);
} private void CreateChartData(Worksheet sheet)
{
//Country
sheet.Range["A1"].Value = "Country";
sheet.Range["A2"].Value = "Cuba";
sheet.Range["A3"].Value = "Mexico";
sheet.Range["A4"].Value = "France";
sheet.Range["A5"].Value = "German"; //Jun
sheet.Range["B1"].Value = "Jun";
sheet.Range["B2"].NumberValue = ;
sheet.Range["B3"].NumberValue = ;
sheet.Range["B4"].NumberValue = ;
sheet.Range["B5"].NumberValue = ; //Jun
sheet.Range["C1"].Value = "Aug";
sheet.Range["C2"].NumberValue = ;
sheet.Range["C3"].NumberValue = ;
sheet.Range["C4"].NumberValue = ;
sheet.Range["C5"].NumberValue = ; //Style
sheet.Range["A1:C1"].Style.Font.IsBold = true;
sheet.Range["A2:C2"].Style.KnownColor = ExcelColors.LightYellow;
sheet.Range["A3:C3"].Style.KnownColor = ExcelColors.LightGreen1;
sheet.Range["A4:C4"].Style.KnownColor = ExcelColors.LightOrange;
sheet.Range["A5:C5"].Style.KnownColor = ExcelColors.LightTurquoise; //Border
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeTop].Color = Color.FromArgb(, , );
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.FromArgb(, , );
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromArgb(, , );
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromArgb(, , );
sheet.Range["A1:C5"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin; sheet.Range["B2:C5"].Style.NumberFormat = "\"$\"#,##0";
} private void ExcelDocViewer(string fileName)
{
try
{
//打开生成的Excel
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
}
}
3、运行代码,可以看到如下结果:
4、总结
上面只是该库很小一部分功能,该库还能对Excel进行分组、支持VBA扩展等功能,该库虽然强大,但是不开源,是商业软件。不过经过了解,Spire.XLS也提供免费版,只不过免费版有一个Excel文档不能超过5个sheet的限制。对于一些小项目,免费版也完全够用可以考虑。至于较大应用,则需要商业版。NOPI是开源的Excel库,也可以方便的进行Excel读写操作且无需安装office,但功能不及Spire.XLS全面。开源和非开源,存在即合理,各有优势。