【发布时间】:2020-02-13 06:01:47
【问题描述】:
我在 excel 使用了 2 个表标签中的数据打印并生成 excel 可见但我的错误是不打印两个表只有一个表打印
我在c#page_Load 中创建简单项目,然后在excel file 中打开Excel 和显示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;
}
关于这个问题的任何想法
关于桑杰·马拉基亚
【问题讨论】:
-
@MichaelRandall 不添加我想创建运行时点击事件
-
@T.S.不显示数据,但我想创建运行时单击事件菜单旅行
-
使用Runtime T4 Template 会更干净。