Aspose.Cells是一个广受赞誉的电子表格组件,支持所有Excel格式类型的操作,用户无需依靠Microsoft Excel也可为其应用程序嵌入读写和处理Excel数据表格的功能。Aspose.Cells可以导入和导出每一个具体的数据,表格和格式,在各个层面导入图像,应用复杂的计算公式,并将Excel的数据保存为各种格式等等---完成所有的这一切功能都无需使用Microsoft Excel 和Microsoft Office Automation。
下面翻译的就是一个导出Excel的数据源。
当你用Aspose.Cells将所有自动数据导入Excel中。适用于Aspose.Cells的导出数据的还有其数据源格式如:
ArrayList。
DataTable。
DataColumn。
DataView。
DataGrid。
DataReader。
GridView。
1:从Array数据源导出
从array数据源导出到sheet里面,调用的ImportArray方法取出列的集合。ImportArray方法里面有如下重载参数:
- Array, 导出的数组对象
- Row number,设置导出的数据从第一行第一个单元格开始
- Column number, 设置导出的数据从第几列开始
- Is vertical,设置布尔值,导出的数据是以水平或垂直输出
//实例化新建一个工作薄对象
Workbook workbook = new Workbook();
//添加一个新的工作表到工作薄中
int i = workbook.Worksheets.Add();
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
//创建一个导出Execl列名的字符串数组
string[] names = new string[] { "laurence chen", "roman korchagin", "kyle huang" };
//从第一行第一列开始导出数据
worksheet.Cells.ImportArray(names, 0, 0, true);
//保存Execl文件
workbook.Save("C:\\DataImport.xls");
2:从ArrayList 数据源导出
从ArrayList数据源导出到sheet里面,调用的ImportArrayList方法取出列的集合。ImportArrayList方法里面有如下重载参数:
- ArrayList, 导出的数组对象
- Row number,设置导出的数据从第一行第一个单元格开始
- Column number, 设置导出的数据从第几列开始
- Is vertical,设置布尔值,导出的数据是以水平或垂直输出
//创建一个arraylist 对象
ArrayList list = new ArrayList();
//添加数据
list.Add("laurence chen");
list.Add("roman korchagin");
list.Add("kyle huang");
list.Add("tommy wang");
//从第一行第一列垂直方向开始导出数据到Excel
worksheet.Cells.ImportArrayList(list, 0, 0, true);
3:从自定义数据对象中导出
//定义一个新的工作薄
Workbook book = new Workbook();
//清除所有的工作薄数据
book.Worksheets.Clear();
//在工作薄上创建一个名为data的工作表
Worksheet sheet = book.Worksheets.Add("Data");
//自定义list
List<WeeklyItem> list = new List<WeeklyItem>();
//添加数据到list 中
list.Add(new WeeklyItem(DateTime.Today.AddDays(7)) { AtYarnStage = 1, InWIPStage = 2, Payment = 3, Shipment = 4, Shipment2 = 5 });
list.Add(new WeeklyItem(DateTime.Today.AddDays(14)) { AtYarnStage = 5, InWIPStage = 9, Payment = 7, Shipment = 2, Shipment2 = 5 });
list.Add(new WeeklyItem(DateTime.Today.AddDays(21)) { AtYarnStage = 7, InWIPStage = 3, Payment = 3, Shipment = 8, Shipment2 = 3 });
//We pick a few columns not all to import to the worksheet
//new string[] { "Date", "InWIPStage", "Shipment", "Payment" } 列的标题名字
sheet.Cells.ImportCustomObjects((System.Collections.ICollection)list,
new string[] { "Date", "InWIPStage", "Shipment", "Payment" },
true,
0,
0,
list.Count,
true,
"dd/mm/yyyy",
false);
//自动适应列宽
book.Worksheets[0].AutoFitColumns();
//保存Excel
book.Save(@"e:\test2\ImportedCustomObjects.xls");
4:从DataTable数据源中导出
//创建一个Products名字的datatable对象
DataTable dataTable = new DataTable("Products");
//添加列名到datatable
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));
//创建一个空行
DataRow dr = dataTable.NewRow();
//把数据填充到空行
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;
//新增行添加到datatable里面
dataTable.Rows.Add(dr);
//Creating another empty row in the DataTable object
dr = dataTable.NewRow();
//Adding data to the row
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;
//Adding filled row to the DataTable object
dataTable.Rows.Add(dr);
<pre name="code" class="csharp">从DataTable导入数据到单元格中,默认为A1列开始 ,true为一个新的标题行
worksheet.Cells.ImportDataTable(dataTable, true, "A1");
4:从DataColumn(.NET)数据源导出
从DataTable 或者 DataView对象中调用ImportDataColumn方法进行导出列的集合,起对应的参数如下:
-
Data table/data view, 导出内容来自DataTableDataView
-
Is field name shown or , 指定DataTable or DataView里面的列名是否导入工作表中第一行
- Row number,设置导出的数据从第一行第一个单元格开始
- Column number, 设置导出的数据从第几列开始
- Insert Rows, 布尔值属性。 是否需要额外的行和数据要添加到工作表中
-
Index of the imported column, 选择那一列作为开头进行导出
//Importing the values of 2nd column of the data table
worksheet.Cells.ImportDataColumn(dataTable, true, 0, 0, 1, false);
5:从DataView数据源导出
从一个DataView数据源,调用的ImportDataView方法导出列的集合:
- Data view, 导出内容来自DataView
- Is field name shown 布尔值属性,是否把dataview里面的列名到入到工作表第一行中
- Row number,设置导出的数据从第一行第一个单元格开始
- Column number, 设置导出的数据从第几列开始
- Insert Rows, 布尔值属性。 是否需要额外的行和数据要添加到工作表中
//将dataview内容导入到工作表中
worksheet.Cells.ImportDataView(dataTable.DefaultView, true, 0, 0, false);
6:从DataGrid(.NET)数据源导出
从DataGrid对象中调用ImportFromDataReader方法进行导出列的集合,起对应的参数如下:
It is possible to import data from a DataGrid by calling the ImportDataGrid method of theCells collection. There are many overloaded versions of theImportDataGrid method but a typical overload takes the following parameters:
- Data grid, 导出内容来自datagrid
- Row number,设置导出的数据从第一行第一个单元格开始
- Column number, 设置导出的数据从第几列开始
- Insert Rows, 布尔值属性。 是否需要额外的行和数据要添加到工作表中
<pre name="code" class="csharp">//将<tt>DataGrid</tt>内容导入到工作表中
worksheet.Cells.ImportDataGrid(dataGrid, 0, 0, false);
7:从OleDbDataReader/SqlDataReader (.NET)数据源导出
从OleDbDataReader orSqlDataReader对象中调用ImportFromDataReader方法进行导出列的集合,起对应的参数如下:
- OleDbDataReader/SqlDataReader, 导出内容来自OleDbDataReader/SqlDataReader
- Is field name shown, 指定OleDbDataReader/SqlDataReader里面的列名是否导入工作表中第一行
- Row number,设置导出的数据从第一行第一个单元格开始
- Column number, 设置导出的数据从第几列开始
- Insert Rows, 布尔值属性。 是否需要额外的行和数据要添加到工作表中
//Importing the contents of the data reader to the worksheet
worksheet.Cells.ImportFromDataReader(dataReader,true,0,0,false);
8:从GridView 数据源导出
从DataGrid对象中调用ImportFromDataReader方法进行导出列的集合,起对应的参数如下:
例如:在webform中,有一个id叫GridView1的GridView的控件在form之间,数据来填充到DataTable中,然后直接把Girdview导出到Excel的工作薄中,代码需要写在页面中的
Page_Load事件中 注意加 ispostback
if (! IsPostBack)
{
<span style="color:#008200;">//</span><span><span><span style="color:#008200;">创建</span></span><span><span><span style="color:#008200;">DataTable对象并添加DataColumn对象</span></span> </span></span><span style="color:#008200;"></span>
DataTable dataTable = new DataTable("Products");
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));
/<span><span style="color:#008200;">/为DataRow对象中的列</span></span>进行赋值
DataRow dr = dataTable.NewRow();
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;
dataTable.Rows.Add(dr);
//继续添加
dr = dataTable.NewRow();
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;
dataTable.Rows.Add(dr);
//设置数据源
GridView1.DataSource = dataTable;
//绑定数据
GridView1.DataBind();
//新建一个工作薄
Workbook workbook = new Workbook();
//得到工作薄的第一列
Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];
//从Gridview控件中导出到Excel 工作薄中
worksheet.Cells.ImportGridView(GridView1, 0, 0, false, true, false);
workbook.Save("e:\\test\\importeddata.xls");
//Save with default format, send the file to user so that he may open the file in
//some application or save it to some location
//workbook.Save(this.Response, "importeddata.xls", ContentDisposition.Attachment, new XlsSaveOptions());
}
原文地址:http://www.aspose.com/docs/display/cellsnet/Importing+Data+to+Worksheets#ImportingDatatoWorksheets-array
翻译的不好,大体就这个意思,大家运用的时候多看看例子。