コード例 #1
0
        protected void btnExport_Click(object sender, EventArgs e)
        {
            System.Data.DataTable dt = new System.Data.DataTable();

            dt.Columns.Add(new System.Data.DataColumn(CategoriesStrings.GetText(@"CategoryId"), typeof(string)));
            dt.Columns.Add(new System.Data.DataColumn(CategoriesStrings.GetText(@"CategoryName"), typeof(string)));
            dt.Columns.Add(new System.Data.DataColumn(CategoriesStrings.GetText(@"CategoryRate"), typeof(string)));
            dt.Columns.Add(new System.Data.DataColumn(CategoriesStrings.GetText(@"SubCategoryId"), typeof(string)));
            dt.Columns.Add(new System.Data.DataColumn(CategoriesStrings.GetText(@"SubCategoryName"), typeof(string)));
            List <CategoryUI> categories = ProductController.GetAllCategoriesAndSubCategories();

            foreach (CategoryUI category in categories)
            {
                int i = 0;
                System.Data.DataRow row = dt.NewRow();
                row[i++] = category.CategoryId;
                row[i++] = category.CategoryName;
                row[i++] = category.CategoryRate;
                row[i++] = category.SubCategoryId;
                row[i++] = category.SubCategoryName;
                dt.Rows.Add(row);
            }

            SpreadsheetWriter ex     = SpreadsheetWriter.FromDataTable(dt, true, true);
            ExcelSheetStyle   _style = new ExcelSheetStyle();

            _style.Alignment.Horizontal = HorizontalAlignment.Center;
            ex.AddStyle(_style);

            Response.Clear();
            Response.AddHeader(@"content-disposition", @"attachment;filename=CategoriesExport_" + DateTime.Now.ToString(@"yyyy_MM_dd_HH_mm_ss") + "." + ex.FileExtension);
            Response.Charset         = @"UTF-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = ex.FileContentType;
            Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
            Response.Write(ex.ToString());
            Response.End();
        }