private void InitData(int index) { PageSettings ps = Config.GetPageSettings(); MyReportViewer.SetPageSettings(ps); MyReportViewer.LocalReport.ReportPath = Config.TemplateLocation(PrintData.PrintTemplate.FileName); Dictionary <string, object> item = new Dictionary <string, object>(); if (PrintData != null && PrintData.Data.Count > 0) { item = PrintData.Data[index]; } else { XMessageBox.Warning("未加载到数据"); return; } MyReportViewer.LocalReport.DataSources.Clear(); MyReportViewer.LocalReport.DataSources.Add(new ReportDataSource(PrintData.PrintTemplate.DataSetName, item.ToDataTable())); var printParams = PrintData.PrintTemplate.PrintParams; if (printParams.Count > 0) { int j = 0; foreach (var parameter in MyReportViewer.LocalReport.GetParameters()) { var db_param = PrintData.PrintTemplate.PrintParams.Find(p => p.PrintParamName == parameter.Name); string fileName = $"{PrintData.PrintTemplate.PrintTemplateID}_{db_param.Value}{index}{j++}"; switch (db_param.ParamType) { case ParamType.BarCode: //创建条形码文件 fileName += "1D"; string value = CanCreateCode(db_param, item, out bool isCreate); if (isCreate == false) { XMessageBox.Error($"值({value})不能生成条形码"); } PrintReport.CreateBarCode(value, fileName, Config.TempleAddress); break; case ParamType.QRCode: //创建二维码 fileName += "2D"; value = CanCreateCode(db_param, item, out isCreate); if (isCreate == false) { XMessageBox.Error($"值({value})不能生成条形码"); } PrintReport.CreateQRCode(value, fileName, Config.TempleAddress); break; case ParamType.Other: MyReportViewer.LocalReport.SetParameters(new ReportParameter (parameter.Name, Convert.ToString(item[db_param.Value]))); continue; case ParamType.SQL: value = "0"; if (item.ContainsKey(db_param.Value)) { value = Convert.ToString(item[db_param.Value]); } MyReportViewer.LocalReport.SetParameters(new ReportParameter (parameter.Name, value)); continue; } var file = Path.Combine(Config.TempleAddress, fileName + ".bmp"); MyReportViewer.LocalReport.SetParameters(new ReportParameter(parameter.Name, "file://" + file)); } } MyReportViewer.SetDisplayMode(DisplayMode.PrintLayout); MyReportViewer.ZoomMode = ZoomMode.Percent; MyReportViewer.ZoomPercent = 100; }
/// <summary> /// 打印 /// </summary> /// <param name="startIndex"></param> /// <returns></returns> private string Print(int startIndex = 0) { try { if (PrintData != null && PrintData.Data.Count > 0) { var item = PrintData.Data[startIndex]; m_currentPageIndex = 0; m_streams = null; reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(PrintData.PrintTemplate.DataSetName, item.ToDataTable())); var printParams = PrintData.PrintTemplate.PrintParams; if (printParams.Count > 0) { int j = 0; foreach (var parameter in reportViewer1.LocalReport.GetParameters()) { var db_param = PrintData.PrintTemplate.PrintParams.Find(p => p.PrintParamName == parameter.Name); string fileName = $"{PrintData.PrintTemplate.PrintTemplateID}_{db_param.Value}{startIndex}{j++}"; switch (db_param.ParamType) { case ParamType.BarCode: //创建条形码文件 fileName += "1D"; string value = CanCreateCode(db_param, item, out bool isCreate); if (isCreate == false) { return($"值({value})不能生成条形码"); } PrintReport.CreateBarCode(value, fileName, Config.TempleAddress); break; case ParamType.QRCode: //创建二维码 fileName += "2D"; value = CanCreateCode(db_param, item, out isCreate); if (isCreate == false) { return($"值({value})不能生成条形码"); } PrintReport.CreateQRCode(value, fileName, Config.TempleAddress); break; case ParamType.Other: reportViewer1.LocalReport.SetParameters(new ReportParameter (parameter.Name, Convert.ToString(item[db_param.Value]))); continue; case ParamType.SQL: value = "0"; if (item.ContainsKey(db_param.Value)) { value = Convert.ToString(item[db_param.Value]); } reportViewer1.LocalReport.SetParameters(new ReportParameter (parameter.Name, value)); continue; } var file = Path.Combine(Config.TempleAddress, fileName + ".bmp"); reportViewer1.LocalReport.SetParameters(new ReportParameter(parameter.Name, "file://" + file)); } reportViewer1.SetDisplayMode(DisplayMode.PrintLayout); reportViewer1.ZoomMode = ZoomMode.Percent; reportViewer1.ZoomPercent = 100; } } else { return("未找到需要打印的数据,请检查SQL或是条件或是著录信息"); } return("已打印"); } catch (Exception ex) { return(ex.Message); } }