Export data in Excel template of

Ideas: the Excel template uploaded to the server; then read the document, write data, save it as a new file

 

Then is the time to export processed data definition string , according to export data; note read data stream to be processed next, or will be reported abnormal

        public Stream GetExcelStream (op_client_template_mapping Model) Model profile model // 
        { 
            String fileURL _BillFileService.GetHttp = () + " / " + model.filePath + " / " + model.fileNames;
             return the WebRequest.Create (fileURL) .GetResponse () .GetResponseStream (); 
        } 
        // convert the stream into a file that can be processed 
        public the MemoryStream StreamToMemoryStream (stream inStream) 
        { 
            the MemoryStream outStream = new new the MemoryStream ();
             const  int bufferLen = 1024;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = instream.Read(buffer, 0, bufferLen)) > 0)
            {
                outstream.Write(buffer, 0, count);
            }
            return outstream;
        }

Probably export data

public void AsposeToExcel<T>(List<T> data, ExcelModel excelModel, op_client_template_mapping mapping, string path)
        {
            try
            {
                Stream sm = GetExcelStream(mapping);
                var ms = StreamToMemoryStream(sm);
                ms.Seek(0, SeekOrigin.Begin); int buffsize = (int)ms.Length; //rs.Length 此流不支持查找,先转为MemoryStream
                byte[] bytes = new byte[buffsize];

                ms.Read(bytes, 0, buffsize);
                Workbook workbook = new Workbook(ms);
                ms.Flush(); ms.Close();
                sm.Flush(); sm.Close();
                Worksheet worksheet = workbook.Worksheets[0]; //工作表

                if (excelModel.Title != null && !string.IsNullOrEmpty(excelModel.Title?.cloumnName))
                {
                    SetTitle(worksheet, excelModel.Title.cloumnName, excelModel.Title.Letter + excelModel.Title.Index);
                }
                SetCellData(worksheet, data, excelModel);

                workbook.Save(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private static void SetTitle(Worksheet sheet, string title, string index)
        {
            sheet.Cells[index].PutValue(title);
        }

        /// <summary>
        /// 填充字段
        /// </summary>
        private static void SetCellData<T>(Worksheet sheet, List<T> data, ExcelModel excelModel)
        {
            var= excelModel.ExcelCloumns.Where noData (IT => it.colunmEnglish.Equals ( "" )); // data not bound to a default value 
            int I = 0 ;
             the foreach (T Item in Data) 
            { 
                the foreach (the PropertyDescriptor PD in TypeDescriptor.GetProperties ( typeof (T))) 
                { 
                    var column = excelModel.ExcelCloumns.Where (IT => it.colunmEnglish.Equals (pd.Name)) FirstOrDefault ();.
                     IF ! (column = null ) // presence of the field on assignment 
                    {
                         int row = column.Index + i;
                        if (pd.PropertyType.ToString() == "System.DateTime")
                        {
                            sheet.Cells[column.Letter + row].PutValue(pd.GetValue(item).ToString());//(column.Letter + row)等于 A2,C2这样赋值
                        }
                        else
                        {
                            sheet.Cells[column.Letter + row].PutValue(pd.GetValue(item));
                        }
                    }
                }
                foreach (var it in noData)//赋值默认值
                {
                    int row = it.Index + i;
                    if (it.cloumnName == "序号")
                    {
                        sheet.Cells[it.Letter + row].PutValue(i+1);
                    }
                    else
                    {
                        sheet.Cells[it.Letter + row].PutValue(it.DefaultValue ?? "");
                    }
                }
                i++;
            }
            sheet.AutoFitColumns();
        } 
        #endregion

 

Guess you like

Origin www.cnblogs.com/shuaimeng/p/11504639.html