如何使导出excel为只读在asp.net中 [英] How to make export excel as Read only in asp.net
本文介绍了如何使导出excel为只读在asp.net中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在将数据表导出到excel。但我希望excel文件只读。
这是我的代码。
Hi Everyone,
I am exporting a datatable to excel. But I want to excel file as read only .
Here is my code.
private void ExporttoExcel(DataTable table)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + AdvisoryNumber + ".xls");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
//HttpContext.Current.Response.Charset = System.Text.Encoding.Unicode.EncodingName;
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
//sets font
HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
//sets the table border, cell spacing, border color, font of the text, background, foreground, font height
HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
"borderColor='#000000' cellSpacing='0' cellPadding='0' " +
"style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
//am getting my grid's column headers
int columnscount = table.Columns.Count;
for (int j = 0; j < columnscount; j++)
{ //write in new column
HttpContext.Current.Response.Write("<Td>");
//Get column headers and make it as bold in excel columns
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write(table.Columns[j].ToString());
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
foreach (DataRow row in table.Rows)
{//write in new row
HttpContext.Current.Response.Write("<TR>");
for (int i = 0; i < table.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<Td>");
HttpContext.Current.Response.Write(row[i].ToString());
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
}
HttpContext.Current.Response.Write("<TR>");
HttpContext.Current.Response.Write("<Td colspan='3' align='right'>");
HttpContext.Current.Response.Write("Total");
HttpContext.Current.Response.Write("</Td>");
HttpContext.Current.Response.Write("<Td>");
HttpContext.Current.Response.Write(TotalAmount.ToString());
HttpContext.Current.Response.Write("</Td>");
HttpContext.Current.Response.Write("<Td colspan='3' align='right'>");
HttpContext.Current.Response.Write("</TR>");
HttpContext.Current.Response.Write("</Table>");
HttpContext.Current.Response.Write("</font>");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
推荐答案
我想你无法从响应流生成只读工作表,但可以使用一些第三方工具来实现,这些工具将操纵生成的Excel工作表。
您可以参考下面的主题。
Response.Content Type Readonly Excel [ ^ ]
生成Excel表格ReadOnly使用ASP.Net [ ^ ]
希望它有所帮助。
Hi,
I think you cannot generate the read only work sheets from a response stream but it can be achieved using some third party tools that will manipulate the generated excel sheets.
you can refer below threads.
Response.Content Type Readonly Excel[^]
Generate Excel Sheet ReadOnly using ASP.Net[^]
hope it helps.
这篇关于如何使导出excel为只读在asp.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文