如何用c语言打开excel:C 导出
如何用c语言打开excel:C 导出IApplication application = excelEngine.Excel;{从数组导出前的 Excel 数据可以通过 ImportArray 方法将数据数组导出到 Excel 工作表。下面的代码示例演示了如何将数据数组导出到 Excel 工作表中,水平方向和垂直方向都是如此。using (ExcelEngine excelEngine = new ExcelEngine)
数组导出到 Excel
有时,可能需要将数据数组插入或修改到 Excel 工作表中的现有数据中。在这种情况下,行数和列数是预先知道的。数组在固定范围时非常有用。
Syncfusion Excel (XlsIO) 库支持将数据数组导出到 Excel 工作表中,水平方向和垂直方向导出均可。此外,还可以导出二维数组。
让我们考虑一个场景,“人均开支”。一个人全年的花费都列在 Excel 工作表中。在这个场景中,你需要在新建一行,添加一个新用户 Paul Pogba 的开销,并更新所有被跟踪人员 12 月的开销。
从数组导出前的 Excel 数据
可以通过 ImportArray 方法将数据数组导出到 Excel 工作表。下面的代码示例演示了如何将数据数组导出到 Excel 工作表中,水平方向和垂直方向都是如此。
using (ExcelEngine excelEngine = new ExcelEngine)
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2016;
//Reads input Excel stream as a workbook
IWorkbook workbook = application.Workbooks.Open(File.OpenRead(Path.GetFullPath(@"../../../Expenses.xlsx")));
IWorksheet sheet = workbook.Worksheets[0];
//Preparing first array with different data types
object expenseArray = new object[14]
{"Paul Pogba" 469.00d 263.00d 131.00d 139.00d 474.00d 253.00d 467.00d 142.00d 417.00d 324.00d 328.00d 497.00d "=SUM(B11:M11)"};
//Inserting a new row by formatting as a previous row.
sheet.InsertRow(11 1 ExcelInsertOptions.FormatAsBefore);
//Import Peter's expenses and fill it horizontally
sheet.ImportArray(expenseArray 11 1 false);
//Preparing second array with double data type
double expensesOnDec = new double[6]
{179.00d 298.00d 484.00d 145.00d 20.00d 497.00d};
//Modify the December month's expenses and import it vertically
sheet.ImportArray(expensesOnDec 6 13 true);
//Save the file in the given path
Stream excelStream = File.Create(Path.GetFullPath(@"Output.xlsx"));
workbook.SaveAs(excelStream);
excelStream.Dispose;
}
将数据数组输出到Excel
CSV 导出到 Excel
逗号分隔值 (CSV) 文件有助于生成列数少、行数多的表格数据或轻量级报告。Excel 格式打开这些文件,更容易读懂数据。
Syncfusion Excel (XlsIO) 库支持在几秒钟内打开和保存 CSV 文件。下面的代码示例演示了如何打开 CSV 文件,并将其保存为 XLSX 文件。最重要的是,数据显示在数字格式的表格中。
using (ExcelEngine excelEngine = new ExcelEngine)
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2016;
//Preserve data types as per the value
application.PreserveCSVDataTypes = true;
//Read the CSV file
Stream csvStream = File.OpenRead(Path.GetFullPath(@"../../../TemplateSales.csv")); ;
//Reads CSV stream as a workbook
IWorkbook workbook = application.Workbooks.Open(csvStream);
IWorksheet sheet = workbook.Worksheets[0];
//Formatting the CSV data as a Table
IListObject table = sheet.ListObjects.Create("SalesTable" sheet.UsedRange);
table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium6;
IRange location = table.Location;
location.AutofitColumns;
//Apply the proper latitude & longitude numerformat in the table
TryAndUpdateGeoLocation(table "Latitude");
TryAndUpdateGeoLocation(table "Longitude");
//Apply currency numberformat in the table column 'Price'
IRange columnRange = GetListObjectColumnRange(table "Price");
if(columnRange != )
columnRange.CellStyle.NumberFormat = "$# ##0.00";
//Apply Date time numberformat in the table column 'Transaction_date'
columnRange = GetListObjectColumnRange(table "Transaction_date");
if(columnRange != )
columnRange.CellStyle.NumberFormat = "m/d/yy h:mm AM/PM;@";
//Sort the data based on 'Products'
IDataSort sorter = table.AutoFilters.DataSorter;
ISortField sortField = sorter. SortFields. Add(0 SortOn. Values OrderBy. Ascending);
sorter. Sort;
//Save the file in the given path
Stream excelStream;
excelStream = File.Create(Path.GetFullPath(@"../../../Output.xlsx"));
workbook.SaveAs(excelStream);
excelStream.Dispose;
}
输入csv文件