コード例 #1
0
    protected void Button4_Click(object sender, EventArgs e)
    {
        CExcelBase Tp_ExcelObj = new CExcelBase();
        CExcelBE   be          = new CExcelBE(1, 1, "100000000", "A1", "B1", "GRAY", true, 12, "", null);

        Tp_ExcelObj.InsertData(be);
    }
コード例 #2
0
 public CExportExcel()
 {
     excelbe = new CExcelBase();
 }
コード例 #3
0
        private ActionResult ExportFile(string fileType)
        {
            List <IModel> lstModels = (List <IModel>)Session[SEARCH_RESULT];

            string     sourceName  = "/ExcelPath/Template/Template.xls";
            string     targetName  = "/ExcelPath/Data/" + this.CurrentController + "." + fileType;
            string     _dir        = Server.MapPath("..\\");
            int        rowStart    = 2;
            int        columnStart = 1;
            CExcelBase excelBase   = null;

            try
            {
                excelBase = new CExcelBase(_dir + sourceName, _dir + targetName);

                //get data from model list
                if (lstModels.Count > 0)
                {
                    //get total column (property)
                    var properties = lstModels.FirstOrDefault().GetType().GetProperties();
                    object[,] data = new object[1 + lstModels.Count, properties.Count()];

                    //Set column's headers
                    int iCol = 0;
                    foreach (var property in properties)
                    {
                        var displayName = property.GetCustomAttributes(typeof(DisplayNameAttribute), false)
                                          .FirstOrDefault() as DisplayNameAttribute;
                        data[0, iCol++] = displayName == null ? property.Name : displayName.DisplayName;
                    }

                    //Set column's values
                    for (int i = 0; i < lstModels.Count; i++)
                    {
                        IModel item = lstModels[i];
                        SetPropertyValue(item, i + 1, data);
                    }

                    //export data to file excel
                    excelBase.IsAutoFixColumn = true;
                    excelBase.IsAutoFixRow    = true;
                    excelBase.ExportRange(rowStart, columnStart, (rowStart + data.GetLength(0) - 1), (columnStart + data.GetLength(1)), data);
                }

                int returnCode = excelBase.SaveFile();

                if (returnCode == 0)
                {
                    return(Json(new { Url = targetName }, JsonRequestBehavior.AllowGet));
                }
            }
            catch
            {
                if (excelBase != null)
                {
                    excelBase.ReleaseExcel();
                }
            }

            return(Json(new { Url = string.Empty }));
        }