示例#1
0
        private void CreateSheet <T>(List <T> list, Workbook workbook, int index, List <int[]> mergeIndex, string sheetName) where T : new()
        {
            Worksheet sheet;

            if (index > 2)
            {
                sheet = workbook.CreateEmptySheet();
            }
            else
            {
                sheet = workbook.Worksheets[index];
            }
            sheet.Name = sheetName;

            foreach (int[] indexs in mergeIndex)
            {
                sheet.Range[indexs[0], indexs[1], indexs[2], indexs[3]].Merge();
            }

            var propertyInfos = AttributesHelper.GetProperties <T>();

            #region 表数据
            int j = 0, k = 0;
            foreach (var property in propertyInfos)
            {
                var attr  = AttributesHelper.GetFieldAttributeDescrition <T, TitleAttribute>(property);
                var title = attr?.Title;
                if (title == null)
                {
                    continue;
                }

                #region 列头
                sheet.Range[1, k + 1].Text = title;
                #endregion

                int i = 0;
                foreach (var l in list)
                {
                    var dyg   = sheet.Range[i + 2, j + 1];
                    var type  = property.PropertyType;
                    var value = property.GetValue(l);
                    if (type == typeof(int) || type == typeof(short) || type == typeof(long) || type == typeof(uint) || type == typeof(byte) ||
                        type == typeof(sbyte) || type == typeof(ulong))
                    {
                        dyg.NumberValue  = double.Parse(property.GetValue(l).ToString());
                        dyg.NumberFormat = "#,##0";
                    }
                    else if (type == typeof(float) || type == typeof(double) || type == typeof(decimal))
                    {
                        dyg.NumberValue  = double.Parse(property.GetValue(l).ToString());
                        dyg.NumberFormat = "#,##0.00";
                    }
                    else if (value == null)
                    {
                        dyg.Text = "";
                    }
                    else
                    {
                        dyg.Text = value.ToString();
                    }
                    //dyg.ColumnWidth = 22;
                    //dyg.Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;//边框
                    //dyg.Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
                    //dyg.Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
                    //dyg.Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;

                    i++;
                }
                j++;
                k++;
            }
            #endregion
        }