Ejemplo n.º 1
0
        public static void WriteToFile <T>(string path, List <T> data)
        {
            path = path.EndsWith(".xls") ? path : path + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";

            var props = AssembleUtil.GetPorpertyNames <T>(flags);

            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet       sheet    = workbook.CreateSheet(Path.GetFileNameWithoutExtension(path));


            Dictionary <int, string> valuePos = new Dictionary <int, string>();

            for (int i = 0; i < props.Count; ++i)
            {
                valuePos.Add(i, props[i]);
            }

            IRow headRow = sheet.CreateRow(0);

            SetRowValue(headRow, valuePos);

            int rowPos = 1;

            foreach (var d in data)
            {
                var row = sheet.CreateRow(rowPos++);
                SetRowValue(row, valuePos, AssembleUtil.GetPropertyValues(d, flags));
            }

            using (FileStream file = new FileStream(path, FileMode.Create))
            {
                workbook.Write(file);
            }
        }
Ejemplo n.º 2
0
        private static string GetValueString <T>(T data)
        {
            StringBuilder value     = new StringBuilder();
            var           objValues = AssembleUtil.GetPropertyValues(data, flags);

            foreach (var v in objValues.Values)
            {
                var temp = v.ToString();
                if (temp.Contains(",") || temp.Contains(" "))
                {
                    temp = string.Format("\"{0}\"", v);
                }
                value.AppendFormat("{0},", temp);
            }
            return(value.ToString().TrimEnd(','));
        }