转 (C#)利用Aspose.Cells组件导入导出excel文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了转 (C#)利用Aspose.Cells组件导入导出excel文件相关的知识,希望对你有一定的参考价值。

Aspose.Cells组件可以不依赖excel来导入导出excel文件:

 导入:

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);
        }      

导出:

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文件的主要内容,如果未能解决你的问题,请参考以下文章

Aspose.Cells导入导出

C# Aspose.Cells 将数据设置为具有格式的 Excel 文件

Aspose Cells Excel模板转PDF Linux中文乱码解决

c# winForm使用Aspose.Cells读取CSV文件中文乱码问题

c# Aspose cells怎么设置单元格格式为日期格式

Aspose Cells Excel模板转PDF Linux中文乱码解决