在server2003 iis上发布程序,导出Excel功能本机可下载,其他机器访问无法下载。
应该是服务器设置的问题,不是程序的问题。
下载代码是这样的
public void outexcel(DataTable table, string tablename)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AddHeader("Content-Disposition",
"attachment; filename=" + System.Web.HttpUtility.UrlEncode(tablename, System.Text.Encoding.UTF8) + ".xls");
string colHeaders = "", ls_item = "";
int i = 0;
DataTable dt = table;
DataRow[] myRow = dt.Select("");
for (i = 0; i < dt.Columns.Count; i++)
{
colHeaders += dt.Columns[i].ColumnName.ToString() + "\t"; }
colHeaders += "\n";
resp.Write(colHeaders);
foreach (DataRow row in myRow)
{
for (i = 0; i < dt.Columns.Count; i++)
{
ls_item += row[i].ToString() + "\t"; }
ls_item += "\n"; resp.Write(ls_item);
ls_item = "";
}
resp.End();
}
应该是服务器设置的问题,不是程序的问题。
下载代码是这样的
public void outexcel(DataTable table, string tablename)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AddHeader("Content-Disposition",
"attachment; filename=" + System.Web.HttpUtility.UrlEncode(tablename, System.Text.Encoding.UTF8) + ".xls");
string colHeaders = "", ls_item = "";
int i = 0;
DataTable dt = table;
DataRow[] myRow = dt.Select("");
for (i = 0; i < dt.Columns.Count; i++)
{
colHeaders += dt.Columns[i].ColumnName.ToString() + "\t"; }
colHeaders += "\n";
resp.Write(colHeaders);
foreach (DataRow row in myRow)
{
for (i = 0; i < dt.Columns.Count; i++)
{
ls_item += row[i].ToString() + "\t"; }
ls_item += "\n"; resp.Write(ls_item);
ls_item = "";
}
resp.End();
}
解决方案 »
- 关于Oauth2.0 服务端的请教各位大侠
- 此操作对该事务的状态无效。
- Repeater与Updatepanel实现无刷新
- SQL查询的问题,理论上哪个速度更快些
- NHibernate一个简单的问题
- 救急。触发器有时稳定。有时不稳定(就是数据不正确)
- datagrid新增数据问题,有难度,分不够可以另开贴加!
- 问题不好叙述,打个比方,有一张图片,图片上面有字,我只想打印字,不打印图片,用程序怎么控制请各位指教---在线ing
- 紧急求救:关于报表的问题?????
- 自己写了FTP服务器,为什么隔一段时间服务会停止?
- 数据库
- 我想实现查询伪码的显示真伪的同时显示此伪码已经被查询了几次,总报错“无法将类型object隐式转换为bool”
实在不行的话,用这个组件试试,这个组件不依赖EXCEL
public static void ExportToTable(System.Data.DataTable dtData, string filename)
{
XlsDocument xls = new XlsDocument();
Worksheet sheet = xls.Workbook.Worksheets.AddNamed(filename);//状态栏标题名称 int rowNum = dtData.Rows.Count; int columnNum = dtData.Columns.Count; int rowIndex = 1; int columnIndex = 0; //生成格式列
ColumnInfo colInfo = new ColumnInfo(xls, sheet);
colInfo.ColumnIndexStart = 0;
colInfo.ColumnIndexEnd = Convert.ToUInt16(columnNum);
colInfo.Width = 20 * 256; sheet.AddColumnInfo(colInfo); //绑定标题
Cell cell = null;
foreach (DataColumn dc in dtData.Columns)
{
columnIndex++; cell = sheet.Cells.AddValueCell(rowIndex, columnIndex, dc.ColumnName);
//cell.HorizontalAlignment = HorizontalAlignments.Centered;
//cell.Font.Weight = FontWeight.Bold;
//cell.PatternColor = Colors.Default30;
//cell.RightLineColor = Colors.Black;
//cell.RightLineStyle = 1;
//cell.BottomLineColor = Colors.Black;
//cell.BottomLineStyle = 1;
//cell.TopLineColor = Colors.Black;
//cell.TopLineStyle = 1; cell.HorizontalAlignment = HorizontalAlignments.Centered;
cell.VerticalAlignment = VerticalAlignments.Centered;
cell.Pattern = 1;
cell.PatternColor = Colors.EgaBlue;
cell.UseBorder = true;
cell.TopLineStyle = 1;
cell.TopLineColor = Colors.Black;
cell.BottomLineStyle = 1;
cell.BottomLineColor = Colors.Black;
cell.LeftLineStyle = 1;
cell.LeftLineColor = Colors.Black;
cell.RightLineStyle = 1;
cell.RightLineColor = Colors.Black;
cell.Font.Bold = true;
cell.Font.Height = 11 * 20;
cell.Font.ColorIndex = 1; } //绑定子项数据
for (int i = 0; i < rowNum; i++)
{ rowIndex++; columnIndex = 0; for (int j = 0; j < columnNum; j++)
{ columnIndex++; cell = sheet.Cells.AddValueCell(rowIndex, columnIndex, dtData.Rows[i][j].ToString());
cell.HorizontalAlignment = HorizontalAlignments.Centered;
cell.TextWrapRight = true;
cell.VerticalAlignment = VerticalAlignments.Top;
if (j == 0)
{
cell.LeftLineColor = Colors.Black;
cell.LeftLineStyle = 1;
} cell.RightLineColor = Colors.Black;
cell.RightLineStyle = 1;
cell.BottomLineColor = Colors.Black;
cell.BottomLineStyle = 1; } } //导出
xls.FileName = filename;
xls.Send();
}