Esempio n. 1
0
 /// <summary>
 /// 写入列名
 /// </summary>
 /// <param name="workbook"></param>
 /// <param name="sheet"></param>
 /// <param name="columnTitle"></param>
 private void WriteColumnTitle(IWorkbook workbook, ISheet sheet, RowData columnTitle, ref int rowCount)
 {
     if (columnTitle == null || columnTitle.Data == null || columnTitle.Data.Count == 0)
     {
         return;
     }
     this.WriteRowData(workbook, sheet, columnTitle, ref rowCount);
     if (OnProcess != null)
     {
         OnProcess(sheet.SheetName, columnTitle, rowCount);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 将数据导出到指定页中
        /// </summary>
        /// <param name="columnTitle"></param>
        /// <param name="data"></param>
        private void ExportSheet(IWorkbook workbook, string sheetName, RowData columnTitle, IList <RowData> data)
        {
            if (workbook == null)
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(sheetName))
            {
                sheetName = UUIDUtils.Fid;
            }
            ISheet sheet    = workbook.CreateSheet(sheetName);
            int    rowCount = 0;

            this.WriteColumnTitle(workbook, sheet, columnTitle, ref rowCount);
            this.WriteData(workbook, sheet, data, ref rowCount);
        }
Esempio n. 3
0
        /// <summary>
        /// 写入一行的数据
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="rowData"></param>
        /// <param name="rowCount"></param>
        private void WriteRowData(IWorkbook workbook, ISheet sheet, RowData rowData, ref int rowCount)
        {
            if (rowData == null || rowData.Data == null || rowData.Data.Count == 0)
            {
                return;
            }
            IRow row = sheet.CreateRow(rowCount);

            ICellStyle cellStyle = null;

            if (rowData.IsHeader)
            {
                cellStyle = this.GetColumnHeaderCellType(workbook);
            }
            else
            {
                cellStyle = this.GetDefaultCellType(workbook);
            }

            int count = rowData.Data.Count;

            for (int i = 0; i < count; ++i)
            {
                CellData cellData = rowData.Data[i];
                if (cellData == null)
                {
                    continue;
                }
                SetCellValue(row, cellStyle, i, cellData);
                //隐藏系统默认列
                if (rowCount == 1)
                {
                    //标题行
                    if (ExcelUtils.DefaultFieldNameList.Contains(cellData.Data.ToString(), new FapStringEqualityComparer()))
                    {
                        sheet.SetColumnHidden(i, true);
                    }
                }
            }
            rowCount++;
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="columnList"></param>
        /// <param name="defaultColumnList"></param>
        /// <returns></returns>
        private List <RowData> GetEntityDataList(string tableName, IEnumerable <FapColumn> columnList)
        {
            //先获取列名
            Dictionary <string, FapColumn> columnMap = new Dictionary <string, FapColumn>();

            foreach (var column in columnList)
            {
                columnMap.Add(column.ColName, column);
                //if (!ExcelUtils.DefaultFieldNameList.Contains(column.ColName))
                //{
                //    columnMap.Add(column.ColName, column);
                //}
            }

            //再根据列,组织数据
            List <RowData> dataToExcel = new List <RowData>();

            IEnumerable <dynamic> dataList = null;

            //自定义sql
            if (exportDataSql.IsMissing())
            {
                exportDataSql = $"select * from {tableName}";
            }
            dataList = _dataAccessor.Query(exportDataSql, null, true);

            if (dataList != null)
            {
                RowData  rowDataToExcel = null;
                CellData cellData       = null;

                foreach (IDictionary <string, object> row in dataList)
                {
                    rowDataToExcel          = new RowData();
                    rowDataToExcel.IsHeader = false;
                    rowDataToExcel.Data     = new List <CellData>();

                    foreach (var item in columnMap)
                    {
                        FapColumn column = item.Value;
                        if (column.CtrlType == FapColumn.CTRL_TYPE_REFERENCE)
                        {
                            cellData = new CellData();

                            string mc = row[item.Key + "MC"].ToStringOrEmpty();
                            if (mc.IsPresent())
                            {
                                string refTable = column.RefTable;
                                string refName  = column.RefName;
                                if (RefTableCache.TryGetValue(refTable, out IEnumerable <FapDict> refDatas))
                                {
                                    if (refDatas.Count(k => k.Name == mc) > 1)
                                    {
                                        string refID   = column.RefID;
                                        string refCode = column.RefCode;
                                        var    refData = refDatas.FirstOrDefault(d => d.Fid == row[item.Key].ToString());
                                        cellData.Data = mc + $"({refData.Code})";
                                    }
                                    else
                                    {
                                        //标准导出
                                        cellData.Data = mc;
                                    }
                                }
                            }
                            else
                            {
                                cellData.Data = "";
                            }

                            cellData.Type = CellDataType.STRING;
                            rowDataToExcel.Data.Add(cellData);
                        }
                        else if (column.CtrlType == FapColumn.CTRL_TYPE_COMBOBOX)
                        {
                            cellData = new CellData();

                            cellData.Data = row[item.Key + "MC"];

                            cellData.Type = CellDataType.STRING;
                            rowDataToExcel.Data.Add(cellData);
                        }
                        else
                        {
                            cellData      = new CellData();
                            cellData.Data = row[item.Key];
                            cellData.Type = CellDataType.STRING;
                            rowDataToExcel.Data.Add(cellData);
                        }
                    }

                    dataToExcel.Add(rowDataToExcel);
                }
            }

            return(dataToExcel);
        }