Ejemplo n.º 1
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);
        }