示例#1
0
        //private void SaveToLocal(string fileName)
        //{
        //    //测试xlsx下载后打不开的问题,暂保存到本地试试--benjamin todo
        //    var path = Path.Combine(PFDataHelper.BaseDirectory, "output", fileName);
        //    var directoryName = Path.GetDirectoryName(path);
        //    PFDataHelper.DeleteFile(path);
        //    PFDataHelper.CreateDirectory(directoryName);
        //    var tmpEx = _exporter as XlsxExport;
        //    if (tmpEx != null)
        //    {
        //        tmpEx.workbook.Save(path);
        //    }
        //}
        public void Download()
        {
            ////var tmpEx = _exporter as XlsxExport;
            //var tmpEx = _exporter as XlsxExport1048576;
            Aspose.Cells.Workbook book = null;
            if (_exporter is XlsxExport1048576)
            {
                book = (_exporter as XlsxExport1048576).workbook;
            }
            if (_exporter is XlsxExport)
            {
                book = (_exporter as XlsxExport).workbook;
            }
            if (book != null)
            {
                //if (PFDataHelper.IsDebug)
                //{
                //    var fileName = string.Format("test_{0}.{1}", _fileName, _suffix);
                //    var tmpFileName = Guid.NewGuid().ToString("N") + DateTime.Now.ToString("yyyyMMddHHmmss") + fileName;
                //    var path = Path.Combine(PFDataHelper.BaseDirectory, "TempFile", tmpFileName);
                //    var directoryName = Path.GetDirectoryName(path);
                //    PFDataHelper.CreateDirectory(directoryName);
                //    //book.Save(path, Aspose.Cells.FileFormatType.Xlsx);
                //    book.Save(path, Aspose.Cells.SaveFormat.Xlsx);
                //}
                PFDataHelper.DownloadExcel(HttpContext.Current, book, string.Format("{0}.{1}", _fileName, _suffix), PFDataHelper.GetConfigMapper().GetNetworkConfig().DownloadSpeed);
            }
            else
            {
                //SaveToLocal("excelPo.xlsx");

                if (_fileStream == null)
                {
                    _fileStream = _exporter.SaveAsStream();
                    //SaveToLocal("excelPoAfterSaveStream.xlsx");
                }
                if (_fileStream != null && _fileStream.Length > 0)
                {
                    //PFDataHelper.DownloadFile(HttpContext.Current, _fileStream, string.Format("{0}.{1}", _fileName, _suffix), 1024 * 1024 * 10);
                    PFDataHelper.DownloadFile(HttpContext.Current, _fileStream, string.Format("{0}.{1}", _fileName, _suffix), PFDataHelper.GetConfigMapper().GetNetworkConfig().DownloadSpeed);
                }
            }

            _exporter.Dispose();
        }
示例#2
0
        public Exporter Export(IExport export)
        {
            if (_title == null)
            {
                _title = new List <List <Column> >();
                _title.Add(new List <Column>());
                EachHelper.EachListHeader(_data, (i, field, type) => _title[0].Add(new Column()
                {
                    title = field, field = field, rowspan = 1, colspan = 1
                }));
            }

            Dictionary <int, int>            currentHeadRow = new Dictionary <int, int>();
            Dictionary <string, List <int> > fieldIndex     = new Dictionary <string, List <int> >();
            Func <int, int> GetCurrentHeadRow = cell => currentHeadRow.ContainsKey(cell) ? currentHeadRow[cell] : 0;
            var             currentRow        = 0;
            var             currentCell       = 0;

            export.Init(_data);

            //生成多行题头
            for (var i = 0; i < _title.Count; i++)
            {
                currentCell = 0;

                for (var j = 0; j < _title[i].Count; j++)
                {
                    var item = _title[i][j];
                    if (item.hidden)
                    {
                        continue;
                    }

                    while (currentRow < GetCurrentHeadRow(currentCell))
                    {
                        currentCell++;
                    }

                    export.FillData(currentCell, currentRow, "title_" + item.field, item.title);

                    if (item.rowspan + item.colspan > 2)
                    {
                        export.MergeCell(currentCell, currentRow, currentCell + item.colspan - 1, currentRow + item.rowspan - 1);
                    }

                    if (!string.IsNullOrEmpty(item.field))
                    {
                        if (!fieldIndex.ContainsKey(item.field))
                        {
                            fieldIndex[item.field] = new List <int>();
                        }
                        fieldIndex[item.field].Add(currentCell);
                    }

                    for (var k = 0; k < item.colspan; k++)
                    {
                        currentHeadRow[currentCell] = GetCurrentHeadRow(currentCell++) + item.rowspan;
                    }
                }
                currentRow++;
            }

            //设置题头样式
            export.SetHeadStyle(0, 0, currentCell - 1, currentRow - 1);

            //设置数据样式
            var dataCount = 0;

            EachHelper.EachListRow(_data, (i, r) => dataCount++);
            export.SetRowsStyle(0, currentRow, currentCell - 1, currentRow + dataCount - 1);

            //填充数据
            EachHelper.EachListRow(_data, (rowIndex, rowData) =>
            {
                EachHelper.EachObjectProperty(rowData, (i, name, value) =>
                {
                    if (fieldIndex.ContainsKey(name))
                    {
                        foreach (int cellIndex in fieldIndex[name])
                        {
                            if (_fieldFormatter.ContainsKey(name))
                            {
                                value = _fieldFormatter[name].Format(value);
                            }
                            export.FillData(cellIndex, currentRow, name, value);
                        }
                    }
                });
                currentRow++;
            });

            _fileStream = export.SaveAsStream();

            _suffix = export.suffix;
            if (string.IsNullOrEmpty(_fileName))
            {
                _fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            }

            return(this);
        }
示例#3
0
        public Exporter NewExport(IExport export)
        {
            if (_title == null)
            {
                _title = new List <List <Column> >();
                _title.Add(new List <Column>());
                EachHelper.EachListHeader(_data, (i, field, type) => _title[0].Add(new Column()
                {
                    title = field, field = field, rowspan = 1, colspan = 1
                }));
            }

            Dictionary <int, int>            currentHeadRow = new Dictionary <int, int>();
            Dictionary <string, List <int> > fieldIndex     = new Dictionary <string, List <int> >();
            Func <int, int> GetCurrentHeadRow = cell => currentHeadRow.ContainsKey(cell) ? currentHeadRow[cell] : 0;
            // var currentRowindex = 2;
            var currentRow  = 2;
            var currentCell = 0;

            export.Init(_data);

            //生成多行题头
            for (var i = 0; i < _title.Count; i++)
            {
                currentCell = 0;

                for (var j = 0; j < _title[i].Count; j++)
                {
                    var item = _title[i][j];
                    if (item.hidden)
                    {
                        continue;
                    }

                    while (currentRow < GetCurrentHeadRow(currentCell) + 2)
                    {
                        currentCell++;
                    }
                    if (item.field != "ck")
                    {
                        export.FillData(currentCell, currentRow, "title_" + item.field, item.title);

                        if (item.rowspan + item.colspan > 2)
                        {
                            export.MergeCell(currentCell, currentRow, currentCell + item.colspan - 1, currentRow + item.rowspan - 1);
                        }

                        if (!string.IsNullOrEmpty(item.field))
                        {
                            if (!fieldIndex.ContainsKey(item.field))
                            {
                                fieldIndex[item.field] = new List <int>();
                            }
                            fieldIndex[item.field].Add(currentCell);
                        }

                        for (var k = 0; k < item.colspan; k++)
                        {
                            currentHeadRow[currentCell] = GetCurrentHeadRow(currentCell++) + item.rowspan;
                        }
                    }
                }
                currentRow++;
            }

            export.FillTitleData(0, 0, "", _toptile);
            export.MergeCell(0, 0, currentCell - 1, 1);
            //设置题头样式
            export.SetHeadStyle(0, 0, currentCell - 1, currentRow - 1);

            //设置数据样式
            var dataCount = 0;

            dataCount = _datatable.Rows.Count;
            //EachHelper.EachListRow(_data, (i, r) => dataCount++);
            export.SetRowsStyle(0, currentRow, currentCell - 1, currentRow + dataCount - 1);

            //填充数据
            //            EachHelper.EachListRow(_data, (rowIndex, rowData) =>
            //            {
            //                EachHelper.EachObjectProperty(rowData, (i, name, value) =>
            //                {
            //                    if (fieldIndex.ContainsKey(name))
            //                        foreach (int cellIndex in fieldIndex[name])
            //                        {
            //                            if (_fieldFormatter.ContainsKey(name))
            //                                value = _fieldFormatter[name].Format(value);
            //                            export.FillData(cellIndex, currentRow, name, value);
            //                        }
            //                });
            //                currentRow++;
            //            });
            int cellIndex = 0;
            int cellnum   = currentCell;
            int h         = 0;

            if (_datatable.Columns.Contains("ck"))
            {
                cellnum++;
                h++;
            }
            if (_datatable.Columns.Contains("ID"))
            {
                cellnum++;
                h++;
            }
            foreach (DataRow row in _datatable.Rows)
            {
                int k = 0;
                for (int i = h; i < cellnum; i++)
                {
                    export.FillData(k, currentRow, "", row[i]);
                    k++;
                }


                currentRow++;
            }
            export.SaveAsStream(Path);

            //_suffix = export.suffix;
            //if (string.IsNullOrEmpty(_fileName))
            //    _fileName = DateTime.Now.ToString("yyyyMMddHHmmss");

            return(this);
        }