Пример #1
0
        /// <summary>
        /// 将List输出到excel
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="stream"></param>
        /// <param name="list"></param>
        /// <param name="columns"></param>
        /// <param name="sheetName"></param>
        public static void ExportExcel <T>(Stream stream, IList <T> list, Dictionary <string, string> columns, string sheetName = "Sheet")
        {
            //创建Excel
            HSSFWorkbook workbook = CreateExcelFile();
            //创建sheet
            HSSFSheet sheet = workbook.CreateSheet(sheetName);

            // 数据
            workbook.CreateRows(sheet, columns, list, sheetName);

            SaveExcelFile(workbook, stream);
        }
Пример #2
0
        /// <summary>
        /// DataTable写入数据流
        /// </summary>
        /// <param name="dt">数据源</param>
        /// <param name="stream">数据流</param>
        /// <param name="columns">DataTable 中的列及Excel中的标题组成的键值对</param>
        /// <param name="sheetName">sheet名称</param>
        public static void ExportExcel(Stream stream, DataTable dt, Dictionary <string, string> columns, string sheetName = "Sheet")
        {
            if (columns == null || columns.Count == 0)
            {
                throw (new ArgumentNullException("columns", "请设置要导出的列名"));
            }
            if (dt == null || dt.Rows.Count == 0)
            {
                throw new ArgumentNullException("dt", "请设置导出的数据");
            }

            HSSFWorkbook workbook = CreateExcelFile();

            workbook.CreateRows(dt.Rows, columns);

            workbook.SaveExcelFile(stream);
        }