问题
In my WPF MVVM Prism application I read some data (via COM-port) from outer device each second (I use System.Windows.Threading.DispatcherTimer for it). Each time I fill the following buffer with the data read:
/// <summary>
/// Represents the point on the chart.
/// </summary>
public class ChartPoint
{
#region Fields
// The value from outer device.
private double _value;
// The date and time when the value was being obtained.
private DateTime _category;
#endregion
#region Constructors
public ChartPoint() { }
public ChartPoint(DateTime category, double value)
{
this.Category = category;
this.Value = value;
}
#endregion
#region Properties
public double Value
{
get { return this._value; }
set
{
if (double.IsNaN(value) || double.IsInfinity(value))
value = 0;
this._value = value;
}
}
public DateTime Category
{
get { return this._category; }
set { this._category = value; }
}
#endregion
}
I need to export to Microsoft Excel the following data:
DateTime currentDate = ChartPoint.Category.Date;
TimeSpan currentTime = ChartPoint.Category.TimeOfDay;
double currentValue = ChartPoint.Value;
The time value must be in user frendly view, for example: 09:21:54 (hh:mm:ss). And here must be added the name of measuring ultrasonic beam. For example:
string measuringBeam = "beam 1";
or
string measuringBeam = "beam 2";
and so on, the total number of beams - eight (8). So the format of each row in MS Excel table must be:
____________________________________________________________
| Current Date | Current Time | Measuring Beam | The Value |
------------------------------------------------------------
| 04.10.2016 | 09:21:54 | beam 1 | 347.25 |
------------------------------------------------------------
. . . . . . . . . . . . . and so on. . . . . . . . . . . .
I think that during each timer tick each next row should be created. So I need to create programmatically MS Excel file containing the table which has the abovementioned format, save this file in a specific folder, and add new rows there. I deem that to increase the export rate should save not per one row, but per ten or even per hundred rows at a time. That is, once created next hundred rows, it is stored in the Excel file. The entire export operation will be run by choosing corresponding item of drop-down menu and in the same way to stop. Each time the export starts a new Excel file for exported data must be created. I havn't work with Excel Object Model before and with VSTO at all. But now encounter with such necessity. I begin to read about VSTO in MSDN but I very cramped by terms of creating of my application. Please help in creating of export to Excel. The good example will be grate the great. Your help will be appreciated highly.
回答1:
You don't need VSTO or interop. XLSX is a zipped collection of XML files that can be created using either the Open XML SDK or a library like EPPLus which you can install simply by adding the appropriate NuGet package. You can even generate the XML files yourself if you like, although a library like EPPlus makes this a lot easier.
You can easily generate an Excel sheet from a datatable or strongly-typed list using the LoadFromDataTable
or LoadFromCollection
classes, eg:
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
//Load the datatable into the sheet, starting from cell A1.
//Print the column names on row 1
//Apply the Dark1 style
ws.Cells["A1"].LoadFromDataTable(tbl, true, TableStyles.Dark1);
pck.SaveAs(new FileInfo(@"c:\temp\MyFile.xlsx"));
}
Using a strongly-typed collection is just as easy:
sheet.Cells["A1"].LoadFromCollection(items, true, TableStyles.Dark1);
The Excel sheet can be saved to any stream. The [Web Application example] shows how you can save the package to a Web application's response stream, thus allowing you to create real Excel files instead of CSV or HTML files with a fake xlsx
extension
来源:https://stackoverflow.com/questions/39847939/how-to-export-datetime-timespan-string-and-double-values-to-excel-from-wpf-mvv