码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-09-13 18:41:05      阅读:450      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   http   color   io   使用   ar   for   

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

标签:winform   style   blog   http   color   io   使用   ar   for   

原文地址:http://www.cnblogs.com/zeroone/p/3970095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!