using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
StringBuilder sb = new StringBuilder();
//Generate Invoice (Bill) Header.
sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'><b>Load Sheet</b></td></tr>");
sb.Append(" </td></tr>");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("</td><td><b>Date: </b>");
sb.Append(DateTime.Now);
s
sb.Append("</td><td><b>Username: </b>");
sb.Append(user);
sb.Append("</td></tr>");
sb.Append("<tr><td ><b>Route: </b>");
sb.Append(route);
sb.Append("<tr><td><b>Date Order Loaded: </b>");
sb.Append(lDate);
sb.Append("</td></tr>");
sb.Append("</table>");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
//Generate Dispatch Sheet Items Grid.
sb.Append("<table border = '1'>");
sb.Append("<tr>");
foreach (DataColumn column in data.Columns)
{
sb.Append("<th style = 'background-color: #D20B0C;color:#000000'>");
sb.Append(column.ColumnName);
sb.Append("</th>");
}
sb.Append("</tr>");
foreach (DataRow row in data.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in data.Columns)
{
sb.Append("<td>");
sb.Append(row[column]);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</tr></table>");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<br />");
sb.Append("<table>");
sb.Append("<tr><td colspan = '2'></td></tr>");
sb.Append("<tr><td><b>Driver Name:___________________</b><br/>");
sb.Append(driver);
sb.Append("</tr></td>");
Page.ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert('Transaction completed successfully'); window.location.href = 'LoadSheet.aspx';", true);
//Export HTML String as PDF.
StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.End();
GridView1.AllowPaging = false;
GridView1.DataBind();
}
这些代码行基本上确保toss_number包含一串数字。有更好/更有效的方法吗?
答案 0 :(得分:1)
另一种方法是使用try
/ except
。如果输入数字,通常会更有效率。
while True:
try:
toss_number = int(input("How many times do you want to toss the coin?\n"))
break
except ValueError:
print('You have not entered a NUMBER.')
引发ValueError
时会打印一条消息,但循环不会中断,因此我们会返回while
循环和try
部分的开头。