Esempio n. 1
0
    public void SaveValue(int rowIndex, int columnIndex, System.DateTime val)
    {
        HSSFCell cell = GetCell(rowIndex, columnIndex, true);

        cell.SetCellValue(val);
        cell.SetAsActiveCell();
    }
Esempio n. 2
0
    public void SaveValue(int rowIndex, int columnIndex, string val)
    {
        HSSFCell cell = GetCell(rowIndex, columnIndex, true);

        cell.SetCellValue(val);
        cell.SetAsActiveCell();
    }
Esempio n. 3
0
    public void SaveValue(int rowIndex, int columnIndex, float val)
    {
        HSSFCell cell = GetCell(rowIndex, columnIndex, true);

        cell.SetCellValue(NPOIEx.Round2D(val, 2));
        cell.SetAsActiveCell();
    }
Esempio n. 4
0
        /// <summary>
        /// 设置主体数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sheet">excel中的sheet页</param>
        /// <param name="row">在第几行插入数据主体</param>
        /// <param name="dataList">数据的list 泛型 T表示对应的Entity 例如 PolicyDetailsShowDto</param>
        /// <param name="fields">  要显示Dto中的变量数组,按照顺序填写</param>
        /// <param name="style">单元格的样式</param>
        /// <param name="serialFlag">是否需要序号</param>
        public static void setBodyValue <T>(HSSFSheet sheet, int row, IList <T> dataList, String[] fields, HSSFCellStyle style, bool serialFlag)
        {
            try
            {
                if (dataList == null)
                {
                    HSSFRow hssfRow = sheet.CreateRow(row) as HSSFRow;

                    HSSFCell cells = hssfRow.CreateCell(0) as HSSFCell;
                    cells.SetCellValue("您所查找的数据不存在!");
                }
                else
                {
                    for (int i = 0; i < dataList.Count; i++)
                    {
                        HSSFRow hssfRow = sheet.CreateRow(row + i) as HSSFRow;
                        hssfRow.Height = 600;
                        HSSFCell cells = null;
                        if (serialFlag)
                        {
                            // 设置序列号
                            cells           = hssfRow.CreateCell(0) as HSSFCell;
                            cells.CellStyle = style;
                            cells.SetCellValue(i + 1);
                        }
                        //cells.SetCellValue(i + 1);
                        //object obj = Activator.CreateInstance(ObjectType);
                        T    obj        = (T)dataList[i];
                        Type objectType = obj.GetType();
                        for (int j = 0; j < fields.Length; j++)
                        {
                            if (serialFlag)
                            {
                                cells = hssfRow.CreateCell(j + 1) as HSSFCell;
                            }
                            else
                            {
                                cells = hssfRow.CreateCell(j) as HSSFCell;
                            }
                            String fieldName = fields[j];
                            Object value     = null;
                            //String textValue = "";
                            if (null != fieldName && !"".Equals(fieldName))
                            {
                                //获取实体类中的属性
                                PropertyInfo property = objectType.GetProperty(fieldName);
                                //获取该属性的值
                                value = property.GetValue(obj, null);

                                if (null != value)
                                {
                                    cells.SetAsActiveCell();
                                    cells.CellStyle = style;
                                    Type propertyType = value.GetType();
                                    cells.SetCellValue(value.ToString());
                                    string propertyName = propertyType.Name;
                                    switch (propertyName)
                                    {
                                    case "String":
                                        cells.SetCellValue(new HSSFRichTextString(value.ToString()));
                                        break;

                                    case "DateTime":
                                        cells.SetCellValue(DateTime.Parse(value.ToString()).ToString("yyyy/MM/dd HH:mm:ss"));
                                        break;

                                    case "Int16":
                                    case "Int32":
                                    case "Int64":
                                    case "Byte":
                                        cells.SetCellValue(Int64.Parse(value.ToString()));
                                        break;

                                    case "Decimal":
                                    case "Double":
                                        cells.SetCellValue(double.Parse(value.ToString()));
                                        break;

                                    case "System.DBNull":    //空值处理
                                        cells.SetCellValue("");
                                        break;

                                    default:
                                        cells.SetCellValue("");
                                        break;
                                    }
                                }
                                else
                                {
                                    cells.SetAsActiveCell();
                                    cells.CellStyle = style;
                                    cells.SetCellValue("");
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                string ErrorMessege = e.Message;
            }
        }