点击导入按钮: private void button1_Click(object sender, EventArgs e) { this.openFileDialog1.Filter = "Excel(*.xlsx)|*.xlsx";//设置打开文件过滤器 this.openFileDialog1.ShowDialog(); string fileName = this.openFileDialog1.FileName; Workbook tt = new Workbook(fileName); DataTable dt = getStudent();//构造一个表结构 DataRow dr;//定义一个数据行 Aspose.Cells.Cells cs = tt.Worksheets[0].Cells; for (int i = 1; i <= cs.Rows.Count - 1; i++) { dr = dt.NewRow(); dr["sno"] = cs[i, 0].StringValue; dr["sname"] = cs[i, 1].StringValue; dr["sex"] = cs[i, 2].StringValue; dt.Rows.Add(dr); } this.dataGridView1.DataSource = dt; } 导出按钮: private void button2_Click(object sender, EventArgs e) { this.saveFileDialog1.Filter = "Excel(*.xlsx)|*.xlsx";//设置打开文件过滤器 this.saveFileDialog1.ShowDialog(); string fileName = this.saveFileDialog1.FileName; Workbook book = new Workbook(); Worksheet sheet1 = book.Worksheets[0]; Aspose.Cells.Cells cell1 = sheet1.Cells; cell1[0, 0].PutValue("编号"); cell1[0, 1].PutValue("姓名"); DataTable dt = prodata(); for (int i = 0; i < dt.Rows.Count - 1; i++) { cell1[i + 1, 0].PutValue(dt.Rows[i]["sno"].ToString()); cell1[i + 1, 1].PutValue(dt.Rows[i]["sname"].ToString()); } book.FileName = fileName; book.Save(fileName); MessageBox.Show("导出成功!"); } private DataTable prodata() { DataTable dt = getStudent(); DataRow dr; for (int i=0; i <= 10; i++) { dr = dt.NewRow(); dr["sno"] = "A" + i; dr["sname"] = "B" + i; dt.Rows.Add(dr); } return dt; } private DataTable getStudent() { DataTable dt = new DataTable(); DataColumn dc = new DataColumn(); dc.ColumnName = "sno"; dc.DataType = typeof(string); dt.Columns.Add(dc); dc = new DataColumn(); dc.ColumnName = "sname"; dc.DataType = typeof(string); dt.Columns.Add(dc); dc = new DataColumn(); dc.ColumnName = "sex"; dc.DataType = typeof(string); dt.Columns.Add(dc); return dt; }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Aspose.Cells;