private void DataBaseToExcel() { //List<Status> statuses = db.StatusDb.ToList(); //List<User> users = db.UserDb.ToList(); //List<Hazards> hazards = db.HarzardsDb.ToList(); List<StatusL> statusLs = db.StatusLDb.ToList(); User user = new User(); Status status = new Status(); Hazards hazard = new Hazards(); StatusL statusL = new StatusL(); PropertyInfo[] userInfo = user.GetType().GetProperties(); PropertyInfo[] statusInfo = status.GetType().GetProperties(); PropertyInfo[] hazardInfo = hazard.GetType().GetProperties(); PropertyInfo[] statusLInfo = statusL.GetType().GetProperties(); object oOpt = System.Reflection.Missing.Value; //for optional arguments Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbooks oWBs = oXL.Workbooks; Microsoft.Office.Interop.Excel.Workbook oWB = oWBs.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[1]; //outputRows is a List<List<object>> int numberOfRows = statusLs.Count(); //users.Count; int numberOfColumns = statusLInfo.Count();//userInfo.Count(); Microsoft.Office.Interop.Excel.Range oRng; for (int i = 0; i < numberOfColumns; i++) { oSheet.Cells[1, i + 1] = statusLInfo[i].Name; oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, i + 1]; oRng.Interior.ColorIndex = 15; } int row = 0; foreach (var newstatus in statusLs) { if (row <= 12000) { row++; continue; } for (int col = 0; col < numberOfColumns; col++) { try { if (!statusLInfo[col].Name.Equals("User")) { oSheet.Cells[row + 2, col + 1] = statusLInfo[col].GetValue(newstatus, null); } } catch { oSheet.Cells[row + 2, col + 1] = "null"; } } row++; } oXL.Visible = true; }
private void LoadPoiUserReadJson(dynamic json, float lon, float lat) { try { if (json.IsDefined("users")) { foreach (var user in json.users) { var isInUserDb = db.UserLDb.Find(user.id); if (isInUserDb == null) { UserL userEntity = new UserL(); userEntity.ID = user.id; userEntity.IDStr = user.idstr; userEntity.Name = user.name; userEntity.Province = user.province; userEntity.City = user.city; userEntity.Location = user.location; userEntity.ProfileImageUrl = user.profile_image_url; userEntity.Gender = user.gender; userEntity.CreatedAt = user.created_at; db.UserLDb.Add(userEntity); db.SaveChanges(); if (user.IsDefined("status")) { var isInStatusDb = db.StatusLDb.Find(user.status.id); if (isInStatusDb == null) { StatusL statusEntity = new StatusL(); statusEntity.CreatedAt = user.status.created_at; statusEntity.ID = user.status.id; statusEntity.Text = user.status.text; if (statusEntity.Text.IndexOf("此微博已被作者删除") <= -1) { statusEntity.Source = user.status.source; if (user.status.IsDefined("thumbnail_pic")) { statusEntity.ThumbnailPictureUrl = user.status.thumbnail_pic; statusEntity.MiddleSizePictureUrl = user.status.bmiddle_pic; statusEntity.OriginalPictureUrl = user.status.original_pic; } statusEntity.Long = lon;// statusEntity.Lat = lat; statusEntity.RepostsCount = int.Parse(user.status.reposts_count); statusEntity.CommentsCount = int.Parse(user.status.comments_count); statusEntity.AttitudeCount = int.Parse(user.status.attitudes_count); statusEntity.User = userEntity; db.StatusLDb.Add(statusEntity); if (Debugger.Launch()&&Debugger.IsLogging()) { count = count +1; Debugger.Log(1, "抓取数据", "StatusID:" + statusEntity.ID + "count:" + count); } db.SaveChanges(); } } } } } } } finally { } }