//初始 Report report1 = new Report(); report1.Clear(); string ReportFileName = GetReportFileName(Rep); //report1.Load(Environment.CurrentDirectory + "\\Report\\" + ReportFileName); report1.Load(Environment.CurrentDirectory + "\\" + ReportFileName); report1.RegisterData(ds); //report1.RegisterData(dtItem, "item");//report1.RegisterData(ds.Tables[1], "Detail"); report1.GetDataSource("Detail").Enabled = true; report1.GetDataSource("Master").Enabled = true; report1.GetDataSource("Material").Enabled = true; report1.GetDataSource("SUM").Enabled = true; DataBand dataBand1 = report1.FindObject("DataItem") as DataBand; if (dataBand1 == null) { Msg.ShowError("数据区对象不存在,检查(DataItem)是否存在!"); return; } PageHeaderBand PageHeader1 = report1.FindObject("PageHeader1") as PageHeaderBand; if (PageHeader1 == null) { Msg.ShowError("页眉对象不存在,检查(PageHeader1)是否存在!"); return; } dataBand1.DataSource = report1.GetDataSource("Detail"); 列标题位置参考对象 TextObject TxtObjRow = report1.FindObject("TextData") as TextObject; if (TxtObjRow == null) { Msg.ShowError("参照对象不存在,检查(TxtObjRow)是否存在!"); return; } float iLeft = TxtObjRow.Left; float iTop = TxtObjRow.Top; float iWidth = TxtObjRow.Width; float iHeight = TxtObjRow.Height; //列位置参考对象 TextObject TextHeadRow = report1.FindObject("TextHead") as TextObject; if (TextHeadRow == null) { Msg.ShowError("参照对象不存在,检查(TextHead)是否存在!"); return; } float iLeft2 = TextHeadRow.Left; float iTop2 = TextHeadRow.Top; float iWidth2 = TextHeadRow.Width; float iHeight2 = TextHeadRow.Height; DataSourceBase frxSource = report1.GetDataSource("Detail"); float AutoWith = 0; if (AtuoSize == true) { AutoWith = (float)198 / (frxSource.Columns.Count -9); } else { AutoWith = 10; } //对数据区的列自动生成,加载到报表中 int k = 0; for (int i = 0; i < frxSource.Columns.Count; i = i + 1) { //重新设置数据源列别名,由于传入别名重复。默认情况,dataTable列是caption 自动对应的fastreport数据源列的别名 frxSource.Columns[i].Alias = frxSource.Columns[i].Name; string col = frxSource.Columns[i].Name; string colCaption = col.Replace("_1", ""); if (col == "ID" || col == "MaterialID" || col == "MaterialCode" || col == "MaterialName" || col == "Grade" || col == "LR" || col == "LenType" || col == "Sort" || col == "Com") { continue; } //if (col == "Row") continue; string sName = "[" + frxSource.Alias + "." + col + "]"; //数据区设置 TextObject txtObject = new TextObject(); txtObject.Name = col; txtObject.Border.Lines = BorderLines.All; txtObject.HorzAlign = HorzAlign.Center; txtObject.VertAlign = VertAlign.Center; txtObject.Font = new Font("宋体", 9.0f); txtObject.EvenStyle = "Style1"; //txtObject.CreateUniqueName(); txtObject.Bounds = new RectangleF(k * Units.Millimeters * AutoWith, 0, Units.Millimeters * AutoWith, iHeight); txtObject.Text = sName; txtObject.HideZeros = true;//当为0,设置空 //设置打印内容格式 if (col == "Sph" || col == "row") { } else { NumberFormat fn = new NumberFormat(); fn.UseLocale = false; fn.DecimalDigits = 1; fn.DecimalSeparator ="null" ; txtObject.Format = fn; } dataBand1.Objects.Add(txtObject); //Head列区设置 TextObject txtObject1 = new TextObject(); txtObject1.Name = "txt" + col; //txtObject1.CreateUniqueName(); txtObject1.Border.Lines = BorderLines.All; txtObject1.HorzAlign = HorzAlign.Center; txtObject1.VertAlign = VertAlign.Center; txtObject1.Font = new Font("宋体", 9.0f); txtObject1.Bounds = new RectangleF((k) * Units.Millimeters * AutoWith, iTop2, Units.Millimeters * AutoWith, iHeight2); txtObject1.Text = colCaption; if (col == "Sph" || col == "row") { txtObject1.Text = "球\\柱"; } PageHeader1.Objects.Add(txtObject1); k++; }