0

请看一下附图。我手里有数据(BatchName),但以附件格式打印这些数据时遇到问题。我不知道从哪里开始或看什么。

我正在使用 VS2010 C#、Winform App,并想从我的应用程序中打印它。此外,由于许可问题,我不允许使用 Crystal Report,而且我在 VS2010 中也没有找到任何其他报告选项。

我想在一张 A4 尺寸的纸上打印 8 组数据,如下所示(在 excel 中创建的图像;如果需要,可以省略数据集之间的额外间距)。

请帮忙 !

编辑

好的。让我解释一下这个场景。我检索了批次列表并将它们显示在 ListView 控件中。此列表视图中的每个批次都包含其自己的详细信息数据,并且每个列表视图项都标记了相应的批次(或者您可以说是 MasterData)。因此,当我选择一个项目时,我会同时获得 MasterData 和 DetailData。现在,我想做的是,每当我从这个列表视图中选择 1 个或多个批次并单击一个按钮时,每个批次的数据都将以类似于附加图像的方式打印。但是所有批次必须在单独的块中并采用 2 列格式以减少纸张要求。

注意:不要关心数据检索部分,只考虑A4尺寸纸张的数据格式和打印。

在此处输入图像描述

4

1 回答 1

2

好 !我通过在 winform 中使用 WebBrowser 控件来完成此操作。我在后面的代码中根据我的需要生成了 HTML 标签,然后设置浏览器控件来呈现文本。尽管前景并不令人印象深刻,但它对我有用。下面是代码

private void GenerateHtmlText(List<TorTeeFileBO> torTeeFileBos)
{
    StringBuilder pageText = new StringBuilder();
    StringBuilder htmlText = new StringBuilder();
    StringBuilder styleText = new StringBuilder();

    List<TorTeeFileBO> allTorteeFilesEven = new List<TorTeeFileBO>();
    List<TorTeeFileBO> singleTorteeFile = new List<TorTeeFileBO>();

    int maxItem = 0;
    bool isDataFound = false;

    if (torTeeFileBos.Count % 2 == 0)
    {
        allTorteeFilesEven = torTeeFileBos;
    }
    else
    {
        for (int i = 0; i < torTeeFileBos.Count; i++)
        {
            if (i == torTeeFileBos.Count - 1)
            {
                singleTorteeFile.Add(torTeeFileBos[i]);
            }
            else
            {
                allTorteeFilesEven.Add(torTeeFileBos[i]);
            }
        }
    }

    pageText.Append("<html><head><title>QC Print</title></head><body>");

    if (torTeeFileBos.Count > 0)
    {
        for (int i = 0; i < allTorteeFilesEven.Count; i += 2)
        {
            maxItem = 0;
            isDataFound = false;

            htmlText.Append("<table>");
            htmlText.Append("<tr>");
            // 1st Row, 1st Column for Batch Name
            htmlText.Append("<td>");
            htmlText.Append("<b>");
            htmlText.Append("QC: ");
            htmlText.Append(cmbUsers.Text);
            htmlText.Append("</b>");
            htmlText.Append("</td>");
            htmlText.Append("<td>");
            htmlText.Append("<b>");
            htmlText.Append(" ; Batch No. : ");
            htmlText.Append(allTorteeFilesEven[i].FriendlyFileName);
            htmlText.Append("</b>");
            htmlText.Append("</td>");
            htmlText.Append("<td width='50px'></td>");
            // 1st Row, 2nd Column for Batch Name
            htmlText.Append("<td>");
            htmlText.Append("<b>");
            htmlText.Append("QC: ");
            htmlText.Append(cmbUsers.Text);
            htmlText.Append("</b>");
            htmlText.Append("</td>");
            htmlText.Append("<td>");
            htmlText.Append("<b>");
            htmlText.Append(" ; Batch No. : ");
            htmlText.Append(allTorteeFilesEven[i+1].FriendlyFileName);
            htmlText.Append("</b>");
            htmlText.Append("</td>");
            htmlText.Append("</tr>");

            StringBuilder tableHtml = new StringBuilder();

            if (allTorteeFilesEven[i].FileData.Count > allTorteeFilesEven[i+1].FileData.Count)
            {
                maxItem = allTorteeFilesEven[i].FileData.Count;
            }
            else if ((allTorteeFilesEven[i].FileData.Count < allTorteeFilesEven[i + 1].FileData.Count) || 
                (allTorteeFilesEven[i].FileData.Count == allTorteeFilesEven[i + 1].FileData.Count))
            {
                maxItem = allTorteeFilesEven[i+1].FileData.Count;
            }

            for (int j = 0; j < maxItem; j++)
            {
                tableHtml.Append("<tr>");
                //1st Column Data
                tableHtml.Append("<td>");
                isDataFound = false;
                try
                {
                    tableHtml.Append(allTorteeFilesEven[i].FileData[j].Rec_Num);
                    isDataFound = true;
                }
                catch
                {
                    tableHtml.Append("");
                    isDataFound = false;
                }
                tableHtml.Append("</td>");
                if(isDataFound)
                {
                    tableHtml.Append("<td>________________</td>");    
                }
                else
                {
                    tableHtml.Append("<td></td>"); 
                }
                //Column Separator
                tableHtml.Append("<td width='50px'></td>");
                //2nd Column Data
                tableHtml.Append("<td>");
                isDataFound = false;
                try
                {
                    tableHtml.Append(allTorteeFilesEven[i+1].FileData[j].Rec_Num);
                    isDataFound = true;
                }
                catch
                {
                    tableHtml.Append("");
                    isDataFound = false;
                }
                tableHtml.Append("</td>");
                if (isDataFound)
                {
                    tableHtml.Append("<td>________________</td>");
                }
                else
                {
                    tableHtml.Append("<td></td>");
                }

                tableHtml.Append("</tr>");
            }
            htmlText.Append(tableHtml);
            htmlText.Append("<tr></tr>");
            htmlText.Append("</table>");
        }

        if (singleTorteeFile.Count > 0)
        {
            StringBuilder singleHtml = new StringBuilder();

            singleHtml.Append("<table>");
            singleHtml.Append("<tr>");
            // 1st Row, 1st Column for Batch Name
            singleHtml.Append("<td>");
            singleHtml.Append("<b>");
            singleHtml.Append("QC: ");
            singleHtml.Append(cmbUsers.Text);
            singleHtml.Append("</b>");
            singleHtml.Append("</td>");
            singleHtml.Append("<td>");
            singleHtml.Append("<b>");
            singleHtml.Append(" ; Batch No. : ");
            singleHtml.Append(singleTorteeFile[0].FriendlyFileName);
            singleHtml.Append("</b>");
            singleHtml.Append("</td>");
            singleHtml.Append("<td width='50px'></td>");
            // 1st Row, 2nd Column for Batch Name
            singleHtml.Append("<td>");
            singleHtml.Append("");
            singleHtml.Append("");
            singleHtml.Append("</td>");
            singleHtml.Append("<td>");
            singleHtml.Append("");
            singleHtml.Append("</td>");
            singleHtml.Append("</tr>");

            StringBuilder singleTableHtml = new StringBuilder();
            for (int j = 0; j < singleTorteeFile[0].FileData.Count; j++)
            {
                singleTableHtml.Append("<tr>");
                singleTableHtml.Append("<td>");
                singleTableHtml.Append(singleTorteeFile[0].FileData[j].Rec_Num);
                singleTableHtml.Append("</td>");
                singleTableHtml.Append("<td>________________</td>");
                singleTableHtml.Append("</tr>");
            }
            singleHtml.Append(singleTableHtml);
            singleHtml.Append("</table>");

            htmlText.Append(singleHtml);
        }
    }

    pageText.Append(styleText);
    pageText.Append(htmlText);
    pageText.Append("</body></html>");

    wbPagePreview.DocumentText = pageText.ToString();
}
于 2013-11-15T07:19:41.413 回答