问题描述:如何用C1实现应用程序与微软Excel的交互
问题解答:不需要安装Microsoft Excel,可以用C1简单的命令来加载工作簿、操作工作表,然后将工作簿保存到一个Excel文件。它用过程C1XLBook对象在代码中把握Excel文件,可以将任何数据从应用中转换到Excel里。OpenXML格式使得它支持对更小的、压缩的XLSX文件进行保存。
关键代码:
- DrillDataPoints GetChartData(C1XLBook book)
- {
- // Get first sheet
- var sheet = book.Sheets[0];
-
- // Get location, date, and cell count
- var location = sheet[1, 1].Value as string;
- var date = (DateTime)sheet[2, 1].Value;
- var count = sheet.Rows.Count - 5;
- label.Text = string.Format("{0}, {1} points", location, count);
-
- // Get values into arrays for charting
- var drillData = new DrillDataPoints(count);
- for (int r = 0; r < count; r++)
- {
- drillData.Temperature[r] = (double)sheet[r + 5, 1].Value;
- drillData.Pressure[r] = (double)sheet[r + 5, 2].Value;
- drillData.Conductivity[r] = (double)sheet[r + 5, 3].Value;
- drillData.Ph[r] = (double)sheet[r + 5, 4].Value;
- drillData.Depth[r] = r;
- }
- drillData.ScaleValues();
-
- // Send data to chart
- return drillData;
- }
复制代码
效果截图:
源码下载:
如何用C1实现应用程序与微软Excel的交互示例 |