首页 > 编程知识 正文

excel跨表格数据同步,不同表格间的数据导入

时间:2023-05-06 06:56:17 阅读:164368 作者:2645

导出到//summary//excel//summary//param name=' obj '/param///summary///excel//summary///导出paramprivatevoidexporttoexcelcommandaction (objectobj ) stringcurrentdatesting=_ select date.tostring ) yyymmdd ' ) race collect.count==0) { MessageBox.Show ('没有可导出的数据! ','警告提示.',MessageBoxButton.OK,MessageBoxImage.Error; 返回; //command parameter=' { binding element name=race grid } ' datagrid grd=objasdatagrid; ExcelPackage ep=new ExcelPackage (; office open XML.excelworkbookworkbook=EP.workbook; office open XML.excelworksheetsheet=workbook.worksheets.add (数据表); #region配置文件属性workbook.Properties.Category='类别'; workbook.Properties.Author='作者'; workbook.Properties.Comments='注释'; workbook.Properties.Company='公司'; workbook.Properties.Keywords='关键字'; workbook.Properties.Manager='管理员'; workbook.Properties.Status='内容状态'; workbook.Properties.Subject='主题'; workbook.Properties.Title='标题'; workbook.properties.last modified by='最后的保存者'; #endregion #region标头int count=0; Foreach(varcingrd.columns ) { count; sheet.Cells[1,count].Value=c.Header; sheet.column(count ).Width=c.Width.Value/5; sheet.Cells[1,count ].style.horizontal alignment=excelhorizontalalignment.center; sheet.Cells[1,count ].style.vertical alignment=excelverticalalignment.center; //上下中央} # endregionexamplehowtoformatcolumn1as numeric//using (excelrangecol=sheet.cells (2,6,1 ) ) //col.style.horizontal alignment=excelhorizontalalignment.right; //} //写入数据,即写入数据集合int rowIndex=1 Foreach(race race in _RaceCollect) { if (race.QH.ToString().StartsWith(CurrentDateSting))//如果期号以今天开头。则输出Excel { rowIndex++; sheet.Cells[rowIndex, 1].Value = race.QH; sheet.Cells[rowIndex, 2].Value = race.D1; SetCellColor(sheet, rowIndex, race, "C1"); sheet.Cells[rowIndex, 3].Value = race.D2; SetCellColor(sheet, rowIndex, race, "C2"); sheet.Cells[rowIndex, 4].Value = race.D3; SetCellColor(sheet, rowIndex, race, "C3"); sheet.Cells[rowIndex, 5].Value = race.D4; SetCellColor(sheet, rowIndex, race, "C4"); sheet.Cells[rowIndex, 6].Value = race.D5; SetCellColor(sheet, rowIndex, race, "C5"); sheet.Cells[rowIndex, 7].Value = race.D6; SetCellColor(sheet, rowIndex, race, "C6"); sheet.Cells[rowIndex, 8].Value = race.D7; SetCellColor(sheet, rowIndex, race, "C7"); sheet.Cells[rowIndex, 9].Value = race.D8; SetCellColor(sheet, rowIndex, race, "C8"); sheet.Cells[rowIndex, 10].Value = race.D9; SetCellColor(sheet, rowIndex, race, "C9"); sheet.Cells[rowIndex, 11].Value = race.D10; SetCellColor(sheet, rowIndex, race, "C10"); sheet.Cells[rowIndex, 12].Value = race.DCount; sheet.Cells[rowIndex, 13].Value = race.Colordiff; sheet.Cells[rowIndex, 14].Value = race.ColordiffTotal; sheet.Cells[rowIndex, 15].Value = race.QH; sheet.Cells[rowIndex, 16].Value = race.P1; sheet.Cells[rowIndex, 17].Value = race.P2; sheet.Cells[rowIndex, 18].Value = race.P3; sheet.Cells[rowIndex, 19].Value = race.P4; sheet.Cells[rowIndex, 20].Value = race.P5; sheet.Cells[rowIndex, 21].Value = race.P6; sheet.Cells[rowIndex, 22].Value = race.P7; sheet.Cells[rowIndex, 23].Value = race.P8; sheet.Cells[rowIndex, 24].Value = race.P9; sheet.Cells[rowIndex, 25].Value = race.P10; } sheet.Cells[rowIndex + 1, 1].Value = "颜色小计"; //利用反射读取类属性的值 var typeRaceTj = typeof(RaceTj); for (int i = 1; i <= 10; i++) { var n = (int)typeRaceTj.GetProperty($"T{i}").GetValue(_Tj); sheet.Cells[rowIndex + 1, i+1].Value = n; } sheet.Row(rowIndex + 1).Height = 30; sheet.Row(1).Height = 30; } var border = sheet.Cells[1, 1, rowIndex+1, 25].Style.Border; border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin; sheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; sheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center; sheet.Cells.Style.WrapText = true; if (rowIndex == 1) { MessageBox.Show("没有选中日期的数据可导出Excel(可能只有前1日数据存在)!", "警告提示.", MessageBoxButton.OK, MessageBoxImage.Error); return; } Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog(); sfd.Filter = "Excel(*.xlsx)|*.xlsx"; sfd.Title = "保存文件路径及名称"; if (sfd.ShowDialog() == true) { FileInfo file = new FileInfo(sfd.FileName); ep.File = file; ep.Save(); MessageBox.Show("选中日期数据已经成功保存至Excel!", "保存Excel提示.", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("保存操作被取消"); } } /// <summary> /// 设置单元格背景色为红色 /// </summary> /// <param name="sheet"></param> /// <param name="rowIndex"></param> /// <param name="race"></param> /// <param name="colorFieldName"></param> private static void SetCellColor(ExcelWorksheet sheet, int rowIndex, Race race, string colorFieldName) { //反射类属性值 PropertyInfo p = typeof(Race).GetProperty(colorFieldName); int col = 0; if ((int)p.GetValue(race) == 1) { col = int.Parse(colorFieldName.Substring(1, colorFieldName.Length - 1)) + 1; sheet.Cells[rowIndex, col].Style.Fill.PatternType = ExcelFillStyle.Solid;//将背景图案设置为实体 sheet.Cells[rowIndex, col].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.IndianRed); } }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。