我有一个用于创建 Excel 的 C# 应用程序。代码如下,
public void CreateExcelSheet()
{
StringBuilder sbWriteBuffer = new StringBuilder();
sbWriteBuffer.Append("<html xmlns:x='urn:schemas-microsoft-com:office:excel'>");
sbWriteBuffer.Append("<head>");
sbWriteBuffer.Append("<meta http-equiv='Content-Type' content='text/html;charset=windows-1252'>");
sbWriteBuffer.Append("<!--[if gte mso 9]>");
sbWriteBuffer.Append("<xml>");
sbWriteBuffer.Append("<x:ExcelWorkbook>");
sbWriteBuffer.Append("<x:ExcelWorksheets>");
sbWriteBuffer.Append("<x:ExcelWorksheet>");
sbWriteBuffer.Append("<x:Name>Sheet1</x:Name>");
sbWriteBuffer.Append("<x:WorksheetOptions>");
sbWriteBuffer.Append("<x:Panes>");
sbWriteBuffer.Append("</x:Panes>");
sbWriteBuffer.Append("</x:WorksheetOptions>");
sbWriteBuffer.Append("</x:ExcelWorksheet>");
sbWriteBuffer.Append("<x:ExcelWorksheet>");
sbWriteBuffer.Append("<x:Name>Sheet2</x:Name>");
sbWriteBuffer.Append("<x:WorksheetOptions>");
sbWriteBuffer.Append("<x:Panes>");
sbWriteBuffer.Append("</x:Panes>");
sbWriteBuffer.Append("</x:WorksheetOptions>");
sbWriteBuffer.Append("</x:ExcelWorksheet>");
sbWriteBuffer.Append("</x:ExcelWorksheets>");
sbWriteBuffer.Append("</x:ExcelWorkbook>");
sbWriteBuffer.Append("</xml>");
sbWriteBuffer.Append("<![endif]-->");
sbWriteBuffer.Append("</head>");
//*************Data shouldbe in worksheet 1
sbWriteBuffer.Append("<body>");
sbWriteBuffer.Append("<table>");
sbWriteBuffer.Append("<tr></tr>");
sbWriteBuffer.Append("<tr style='background-color:brown;border:solid;border-width:1px'>");
sbWriteBuffer.Append("<th>" + "Details of customers on 11/4/2014" + "</th>");
sbWriteBuffer.Append("</tr>");
sbWriteBuffer.Append("<tr><td>ID</td><td>Name</td><td>Balance</td></tr>");
sbWriteBuffer.Append("<tr><td>1234</td><td>Al Bundy</td><td>45</td></tr>");
sbWriteBuffer.Append("<tr><td>1234</td><td>Chris Mary</td><td>20</td></tr>");
sbWriteBuffer.Append("<tr></tr>");
sbWriteBuffer.Append("</table>");
sbWriteBuffer.Append("</body>");
//*************************************************
//*************Data shouldbe in worksheet 2
sbWriteBuffer.Append("<body>");
sbWriteBuffer.Append("<table>");
sbWriteBuffer.Append("<tr></tr>");
sbWriteBuffer.Append("<tr>");
sbWriteBuffer.Append("<tr style='background-color:brown;border:solid;border-width:1px'>");
sbWriteBuffer.Append("<th>" + "Details of customers on 11/3/2014" + "</th>");
sbWriteBuffer.Append("</tr>");
sbWriteBuffer.Append("<tr><td>ID</td><td>Name</td><td>Balance</td></tr>");
sbWriteBuffer.Append("<tr><td>9876</td><td>Homer Simpson</td><td>-129</td></tr>");
sbWriteBuffer.Append("<tr><td>5555</td><td>Peter Griffin</td><td>0</td></tr>");
sbWriteBuffer.Append("<tr></tr>");
sbWriteBuffer.Append("</table>");
sbWriteBuffer.Append("</body>");
//***************************************************
sbWriteBuffer.Append("</html>");
System.IO.File.WriteAllText(@"D:\excel.xls", sbWriteBuffer.ToString());
}
问题是我需要将数据打印在 2 个 Excel 文件的工作表上。但现在我在同一张工作表上写了它。为了达到我的要求,我需要在此代码中进行哪些修改?如何在 C# 中的多个工作表上打印数据?
在上面的相同代码中,如何指定一组数据以显示在特定工作表中?如何在上述代码中将工作表关联/连接到表?