繁体   English   中英

winform-如何用C#在excel中打印多个表格标签

[英]winform-how to print multiple table tag in excel with C#

我在excel 2 表标签中打印数据并生成可见的excel但我的错误是不打印两个表只有一个表打印我在c# page_Load创建简单项目然后打开Excel并在excel file显示two table但会出现一些错误只生成一张表显示

我的代码

 private void Form2_Load(object sender, EventArgs e)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.Text.StringBuilder sb1 = new System.Text.StringBuilder();

        Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
        Workbook xlWorkBook = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        sb.Append("<html>");
        sb.Append("<head></head>");
        sb.Append("<body>");
        sb.Append("<table border=1>");
        sb.Append("<tr style=height:30px;>");
        sb.Append("<td style='min-width:50px;text-align:center;font-size:20px;background-color:#c0c0c0;' colspan=6>");
        sb.Append("<nobr><b>Meru Malakiya</b></nobr>");
        sb.Append("</td>");
        sb.Append("</tr>");
        sb.Append("<tr style='height:30px;'>");
        sb.Append("<td style='min-width:50px;text-align:center'>");
        sb.Append("<nobr><b>No.</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style=min-width:50px;text-align:center>");
        sb.Append("<nobr><b>D 3</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style='text-align:center;font-size:14px;background-color:#c0c0c0;min-width:50px' colspan='2'>");
        sb.Append("<nobr><b>DEAF-Claim Principal</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style='text-align:center;font-size:14px;background-color:#c0c0c0;min-width:50px'>");
        sb.Append("<nobr><b>G.L.</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style='text-align:center;font-size:14px;min-width:50px'>");
        sb.Append("<nobr><b>Head Office</b></nobr>");
        sb.Append("</td>");
        sb.Append("</tr>");


        sb.Append("</table></body></html>");



        sb1.Append("<html>");
        sb1.Append("<head></head>");
        sb1.Append("<body>");
        sb1.Append("<table border=1>");
        sb1.Append("<tr style=height:30px;>");
        sb1.Append("<td style='min-width:50px;text-align:center;font-size:20px;background-color:#c0c0c0;' colspan=6>");
        sb1.Append("<nobr><b>Sanjay Malakiya</b></nobr>");
        sb1.Append("</td>");
        sb1.Append("</tr>");
        sb1.Append("<tr style='height:30px;'>");
        sb1.Append("<td style='min-width:50px;text-align:center'>");
        sb1.Append("<nobr><b>No.</b></nobr>");
        sb1.Append("</td>");
        sb1.Append("<td style=min-width:50px;text-align:center>");
        sb1.Append("<nobr><b>D 3</b></nobr>");
        sb1.Append("</td>");
        sb1.Append("<td style='text-align:center;font-size:14px;background-color:#c0c0c0;min-width:50px' colspan='2'>");
        sb1.Append("<nobr><b>DEAF-Claim Principal</b></nobr>");
        sb1.Append("</td>");
        sb1.Append("<td style='text-align:center;font-size:14px;background-color:#c0c0c0;min-width:50px'>");
        sb1.Append("<nobr><b>G.L.</b></nobr>");
        sb1.Append("</td>");
        sb1.Append("<td style='text-align:center;font-size:14px;min-width:50px'>");
        sb1.Append("<nobr><b>Head Office</b></nobr>");
        sb1.Append("</td>");
        sb1.Append("</tr>");


        sb1.Append("</table></body></html>");
        String Todaysdate1 = DateTime.Now.ToString("dd-MM-yyyy");
        if (!Directory.Exists("C:\\Users\\test\\Desktop\\" + Todaysdate1))
        {
            Directory.CreateDirectory("C:\\Users\\test\\Desktop\\" + Todaysdate1);
        }
        using (System.IO.StreamWriter file1 = new System.IO.StreamWriter("C:\\Users\\test\\Desktop\\" + Todaysdate1 + "\\Print Voucher.XLS"))
        {
           // file1.WriteLine(sb.ToString());
            file1.WriteLine(sb1.ToString());
        }
        using (System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Users\\test\\Desktop\\" + Todaysdate1 + "\\Print Voucher.XLS"))
        {
            file.WriteLine(sb.ToString());

        }
        Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
        Workbook xlWorkBook1 = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        xlWorkBook1 = Excel.Workbooks.Open("C:\\Users\\test\\Desktop\\" + Todaysdate1 + "\\Print Voucher.XLS", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
       Microsoft.Office.Interop.Excel.Worksheet Wt1 = (Excel.Worksheet)xlWorkBook1.Worksheets.get_Item(1);
        Excel.Visible = true;

    }

不打印两张表只显示一张表

对这个问题有什么想法

桑杰·马拉基亚

1 个文件仅使用 Html 标签一次,多次使用 Table 标签

我的代码:-

private void Form2_Load(object sender, EventArgs e)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.Text.StringBuilder sb1 = new System.Text.StringBuilder();

        Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
        Workbook xlWorkBook = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        sb.Append("<html>");
        sb.Append("<head></head>");
        sb.Append("<body>");
        sb.Append("<table border=1>");
        sb.Append("<tr style=height:30px;>");
        sb.Append("<td style='min-width:50px;text-align:center;font-size:20px;background-color:#c0c0c0;' colspan=6>");
        sb.Append("<nobr><b>Meru Malakiya</b></nobr>");
        sb.Append("</td>");
        sb.Append("</tr>");
        sb.Append("<tr style='height:30px;'>");
        sb.Append("<td style='min-width:50px;text-align:center'>");
        sb.Append("<nobr><b>No.</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style=min-width:50px;text-align:center>");
        sb.Append("<nobr><b>D 3</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style='text-align:center;font-size:14px;background-color:#c0c0c0;min-width:50px' colspan='2'>");
        sb.Append("<nobr><b>DEAF-Claim Principal</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style='text-align:center;font-size:14px;background-color:#c0c0c0;min-width:50px'>");
        sb.Append("<nobr><b>G.L.</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style='text-align:center;font-size:14px;min-width:50px'>");
        sb.Append("<nobr><b>Head Office</b></nobr>");
        sb.Append("</td>");
        sb.Append("</tr>");
        sb.Append("</table>");

        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append("<br/>");
        sb.Append("<br/>");




        sb.Append("<table border=1>");
        sb.Append("<tr style=height:30px;>");
        sb.Append("<td style='min-width:50px;text-align:center;font-size:20px;background-color:#c0c0c0;' colspan=6>");
        sb.Append("<nobr><b>Sanjay Malakiya</b></nobr>");
        sb.Append("</td>");
        sb.Append("</tr>");
        sb.Append("<tr style='height:30px;'>");
        sb.Append("<td style='min-width:50px;text-align:center'>");
        sb.Append("<nobr><b>No.</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style=min-width:50px;text-align:center>");
        sb.Append("<nobr><b>D 3</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style='text-align:center;font-size:14px;background-color:#c0c0c0;min-width:50px' colspan='2'>");
        sb.Append("<nobr><b>DEAF-Claim Principal</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style='text-align:center;font-size:14px;background-color:#c0c0c0;min-width:50px'>");
        sb.Append("<nobr><b>G.L.</b></nobr>");
        sb.Append("</td>");
        sb.Append("<td style='text-align:center;font-size:14px;min-width:50px'>");
        sb.Append("<nobr><b>Head Office</b></nobr>");
        sb.Append("</td>");
        sb.Append("</tr>");

       // ClientScript.RegisterStartupScript(this.GetType(), "PrintExcel", spPrint.ToString());
        sb1.Append("</table></body></html>");
        String Todaysdate1 = DateTime.Now.ToString("dd-MM-yyyy");
        if (!Directory.Exists("C:\\Users\\test\\Desktop\\" + Todaysdate1))
        {
            Directory.CreateDirectory("C:\\Users\\test\\Desktop\\" + Todaysdate1);
        }
        using (System.IO.StreamWriter file1 = new System.IO.StreamWriter("C:\\Users\\test\\Desktop\\" + Todaysdate1 + "\\Print Voucher.XLS"))
        {
            file1.WriteLine(sb.ToString());
           // file1.WriteLine(sb1.ToString());
        }

        Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
        Workbook xlWorkBook1 = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        xlWorkBook1 = Excel.Workbooks.Open("C:\\Users\\test\\Desktop\\" + Todaysdate1 + "\\Print Voucher.XLS", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
       Microsoft.Office.Interop.Excel.Worksheet Wt1 = (Excel.Worksheet)xlWorkBook1.Worksheets.get_Item(1);
        Excel.Visible = true;

    }

输出

仅一次在 <code>C#</code> 表单中使用 <code>Sb.Append("<html>")</code> 标签

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM