首页 > 代码库 > (C#)利用Aspose.Cells组件导入导出excel文件
(C#)利用Aspose.Cells组件导入导出excel文件
Aspose.Cells组件可以不依赖excel来导入导出excel文件:
导入:
[csharp] view plain copy print?
- public static System.Data.DataTable ReadExcel(String strFileName)
- {
- Workbook book = new Workbook();
- book.Open(strFileName);
- Worksheet sheet = book.Worksheets[0];
- Cells cells = sheet.Cells;
- return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);
- }
public static System.Data.DataTable ReadExcel(String strFileName) { Workbook book = new Workbook(); book.Open(strFileName); Worksheet sheet = book.Worksheets[0]; Cells cells = sheet.Cells; return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true); }
导出:
[csharp] view plain copy print?
- private static void Export<T>(IEnumerable<T> data, HttpResponse response)
- {
- Workbook workbook = new Workbook();
- Worksheet sheet = (Worksheet)workbook.Worksheets[0];
- PropertyInfo[] ps = typeof(T).GetProperties();
- var colIndex = "A";
- foreach (var p in ps)
- {
- sheet.Cells[colIndex + 1].PutValue(p.Name);
- int i = 2;
- foreach (var d in data)
- {
- sheet.Cells[colIndex + i].PutValue(p.GetValue(d, null));
- i++;
- }
- colIndex = ((char)(colIndex[0] + 1)).ToString();
- }
- response.Clear();
- response.Buffer = true;
- response.Charset = "utf-8";
- response.AppendHeader("Content-Disposition", "attachment;filename=xxx.xls");
- response.ContentEncoding = System.Text.Encoding.UTF8;
- response.ContentType = "application/ms-excel";
- response.BinaryWrite(workbook.SaveToStream().ToArray());
- response.End();
- }
private static void Export<T>(IEnumerable<T> data, HttpResponse response) { Workbook workbook = new Workbook(); Worksheet sheet = (Worksheet)workbook.Worksheets[0]; PropertyInfo[] ps = typeof(T).GetProperties(); var colIndex = "A"; foreach (var p in ps) { sheet.Cells[colIndex + 1].PutValue(p.Name); int i = 2; foreach (var d in data) { sheet.Cells[colIndex + i].PutValue(p.GetValue(d, null)); i++; } colIndex = ((char)(colIndex[0] + 1)).ToString(); } response.Clear(); response.Buffer = true; response.Charset = "utf-8"; response.AppendHeader("Content-Disposition", "attachment;filename=xxx.xls"); response.ContentEncoding = System.Text.Encoding.UTF8; response.ContentType = "application/ms-excel"; response.BinaryWrite(workbook.SaveToStream().ToArray()); response.End(); }
http://blog.csdn.net/weiky626/article/details/7514637
(C#)利用Aspose.Cells组件导入导出excel文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。