Winform之SpreadSheetGear转DevExpress.XtraSpreadsheet.v13.2 z

DevExpress.XtraSpreadsheet.v13.2 允许用户创建、管理、打印、转换spreadsheet文件而不需要用户安装Office。

什么是Spreadsheet
可以看到最后就是一个近乎完整的Excel界面。
如何使用Spreadsheet


项目添加引用:
DevExpress.Docs.v13.2
DevExpress.XtraSpreadsheet.v13.2

代码示例:

注释掉的部分是原来使用SpreadSheetGear的代码,理论上DevExpress.Spreadsheet可以完全替代SpreadSheetGear。

 

using DevExpress.Spreadsheet;
using DevExpress.Spreadsheet.Export;


//打开Excel文件
//SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(openFileDialog.FileName, System.Globalization.CultureInfo.CurrentCulture);
spreadsheetControl1.LoadDocument(openFileDialog.FileName, DocumentFormat.Xls);


//从流中读取数据
var stream = new MemoryStream();
gridView1.ExportToXls(stream, true); //生成流
//var workbook = SpreadsheetGear.Factory.GetWorkbookSet().Workbooks.OpenFromStream(stream);
Workbook workbook = new Workbook();
workbook.LoadDocument(stream, DocumentFormat.Xls);


//保存Excel文件
//workbook.SaveAs(saveFileDialog.FileName, SpreadsheetGear.FileFormat.XLS97);
workbook.SaveDocument(saveFileDialog.FileName, DocumentFormat.Xls);


//新建Workbook和Worksheet
//SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
//SpreadsheetGear.IWorksheet workSheet = workbook.Worksheets[0];
//workSheet.Name = dtSource.TableName;
Workbook workbook = new Workbook();
Worksheet workSheet = workbook.Worksheets.Add(dtSource.TableName);	


//将DataTable填到Cell中
//SpreadsheetGear.IRange range = workSheet.Cells["A2"];
//range.CopyFromDataTable(dtSource, SpreadsheetGear.Data.SetDataFlags.NoColumnHeaders);
workSheet.Import(dtSource, false, 1, 0);


//数据导出到DataTable
//var dataSet = workbook.GetDataSet(SpreadsheetGear.Data.GetDataFlags.FormattedText |
//                        SpreadsheetGear.Data.GetDataFlags.NoColumnHeaders);
Worksheet worksheet = workbook.Worksheets[0];
Range range = worksheet.GetUsedRange();
bool rangeHasHeaders = false;
DataTable dataTable = worksheet.CreateDataTable(range, rangeHasHeaders); 
//务必添加引用DevExpress.Docs.v13.2.dll
DataTableExporter exporter = worksheet.CreateDataTableExporter(range, ataTable, rangeHasHeaders);
exporter.Export();


//设置Cell值和格式
//workSheet.Cells[0, index].Columns.AutoFit();
//workSheet.Cells[0, index].Interior.Color = Color.Gray;
//workSheet.Cells[0, index].Borders.LineStyle = SpreadsheetGear.LineStyle.Continuous;
workSheet.Columns.AutoFit(0, dtSource.Columns.Count); 
workSheet.Cells[0, index].FillColor = Color.Gray; 
workSheet.Cells[0, index].Borders.SetAllBorders(Color.Black, BorderLineStyle.Thin);

 

Winform之SpreadSheetGear转DevExpress.XtraSpreadsheet.v13.2 z

上一篇:快速配置 Samba 将 Linux 目录映射为 Windows 驱动器,用于跨平台编程


下一篇:Windows 和 Linux 下生成以当前时间命名的文件