Aspose.Cells处理EXCEL数据
2013-11-08 00:33
393 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Aspose.Cells; namespace AsposeTest { public partial class ExcelForm : Form { public ExcelForm() { InitializeComponent(); } private string InputFile = ""; private string OutputFile = ""; private Workbook book = null; private Worksheet sheet = null; private Workbook inbook = null; private Worksheet insheet = null; private void btnOpenFile_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog() == DialogResult.OK) { tbInputFile.Text = dlg.FileName; InputFile = dlg.FileName; } } private DataTable ProcessInputFile(string file) { Workbook book = new Workbook(); book.Open(InputFile); Worksheet sheet = book.Worksheets[0]; Cells cells = sheet.Cells; //获取excel中的数据保存到一个datatable中 DataTable dt_Import = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, false); // dt_Import. return dt_Import; } private void btnOutput_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "导出Excel (*.xls)|*.xls|Word (*.doc)|*.doc"; saveFileDialog1.Filter = "导出Excel (*.xls)|*.xls|Word (*.doc)|*.doc"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; saveFileDialog1.CreatePrompt = true; saveFileDialog1.Title = "导出文件保存路径"; //saveFileDialog1.ShowDialog(); //string strName = saveFileDialog1.FileName; //设置默认文件类型显示顺序 //saveFileDialog1.FilterIndex = 2; //保存对话框是否记忆上次打开的目录 saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { OutputFile = saveFileDialog1.FileName; ProcessInputFile(InputFile, OutputFile); } } private void ProcessInputFile(string inputFile, string outputFile) { book = new Workbook(); //新建Excel sheet = book.Worksheets[0]; //新建sheet bool OK_NO = DatatableToExcel(""); if (OK_NO) { MessageBox.Show("导出成功", "*^_^* 温馨提示信息", MessageBoxButtons.OK); } else { } } //导出------------下一篇会用到这个方法 public Boolean DatatableToExcel(string data) { Boolean yn = false; try { sheet.Name = "测试"; sheet.Cells.Merge(0, 0, 1, 5); sheet.Cells.Merge(1, 0, 1, 5); Cell cell1 = sheet.Cells[0, 0]; cell1.PutValue("标题是"); // cell1.Style.HorizontalAlignment = TextAlignmentType.Center; // cell1.Style.Font.Name = "黑体"; // cell1.Style.Font.Size = 14; // cell1.Style.Font.IsBold = true; Cell cell2 = sheet.Cells[1, 0]; cell2.PutValue("查询时间:" + DateTime.Now.ToLocalTime()); // cell2.SetStyle(Font); Cell cell = null; for (int col = 0; col < 5; col++) { cell = sheet.Cells[2, col]; cell.PutValue(string.Format("第{0}列",col)); // cell.SetStyle(.IsBold = true; } for (int r = 0; r < 4; r++) { for (int c = 0; c < 5; c++) { sheet.Cells[r + 3, c].PutValue((r * c).ToString()); } } sheet.AutoFitColumns(); sheet.AutoFitRows(); book.Save(OutputFile); yn = true; return yn; } catch (Exception e) { return yn; } } } }
http://www.cnblogs.com/wuhuacong/
相关文章推荐
- 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能
- 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能
- Aspose.Cells 读取Excel数据到DataTable
- c#使用aspose.cells 从datatable导出数据到excel
- 用Aspose.Cells.dll从Excel读取数据到DataSet
- 利用Aspose.Cells 组件导出数据到excel
- Aspose.Cells 根据Excel模板导出数据统计
- 使用Aspose.Cells.dll导出数据到Excel
- 利用Aspose.Cells和Excel模板导出复杂的统计数据
- MVC利用Aspose.Cells.dll实现Excel数据导入数据库
- Aspose.Cells导入数据到Excel,支持单元格操作
- Aspose.Cells 使用整理:读取Excel文件里的数据
- 基于 Aspose.Cells与XML导入excel 数据----操作类封装
- c#使用aspose.cells 从datatable导出数据到excel
- c#使用aspose.cells 从datatable导出数据到excel
- python3.6安装openpyxl用于处理excel数据
- 通过异类查询(OpenDataSource - OLEDB引擎驱动)把外部数据(Excel,TXT)导入到SqlServer及job代理执行出错的处理
- 使用jxl.jar处理excel文档,创建的excel文件不能写入数据
- 在Web界面中实现Excel数据大量导入的处理方式
- [***码农]当Excel数据慌乱数据 导入数据库后或转换数据的处理办法