紧急求助:导出的EXCEL在excel97下打开出错?

glassy2000 2012-04-04 10:10:10
我用如下方法把gridview中的数据导出到EXCEL时,在office2003下很正常的可打开,但换了office2007时打开出错,
错误提示:Excel 无法打开文件“test[2].xls”,因为文件格式或文件扩展名无效......
我换了.xlsx扩展名也不行。

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=test.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
...全文
149 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
传递正能量 2012-04-04
  • 打赏
  • 举报
回复
EXCEL97没有用过,lz说的可是07?
  • 打赏
  • 举报
回复
兄台,你的代码我用了报错!这是我网上查的。。。 一运行就提示下载,可以用0ffice2007打开
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
传递正能量 2012-04-04
  • 打赏
  • 举报
回复
不要用这种方式

用下面这种,winform的,你可以自己修改

public void GetStudentInfoDataToExcel(DataGridView dgvStuInfor,int ype)
{
Microsoft.Office.Interop.Excel.Application excel =
new Microsoft.Office.Interop.Excel.Application();
excel.SheetsInNewWorkbook = 1;
excel.Workbooks.Add();

//设置Excel列名
excel.Cells[1, 1] = "学号";
excel.Cells[1, 2] = "密码";
excel.Cells[1, 3] = "姓名";
excel.Cells[1, 4] = "性别";
excel.Cells[1, 5] = "年级";
excel.Cells[1, 6] = "电话";
excel.Cells[1, 7] = "地址";
excel.Cells[1, 8] = "出生年月日";
excel.Cells[1, 9] = "邮箱";
excel.Cells[1, 10] = "身份证号";

//获取标题行的单元格,即Range
Range range = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 10]);
//设字体加粗
range.Font.Bold = true;
//设置字体颜色
range.Font.ColorIndex = 0;
//设置背景颜色
range.Interior.ColorIndex = 15;
//设置边框样式
range.Borders.LineStyle = XlLineStyle.xlContinuous;

//循环将DataGridView中的数据赋值到Excel中
int i = 0, j = 0;
for (i = 0; i < dgvStuInfor.Rows.Count; i++)
{
for (j = 0; j < 3; j++)
{
excel.Cells[i + 2, j + 1] = dgvStuInfor.Rows[i].Cells[j].Value.ToString();
}
//设置性别
excel.Cells[i + 2, 4] =
dgvStuInfor.Rows[i].Cells["Gender"].Value.ToString() == "False" ? "男" : "女";

//设置显示的学生年级
DataGridViewComboBoxCell dgvCbo =
(DataGridViewComboBoxCell)dgvStuInfor.Rows[i].Cells["GradeId"];
excel.Cells[i + 2, 5] = dgvCbo.FormattedValue.ToString();

for (j = 5; j < 10; j++)
{
excel.Cells[i + 2, j + 1] = dgvStuInfor.Rows[i].Cells[j].Value.ToString();
}
}
//设置出生年月日的格式
excel.get_Range(excel.Cells[2, 8], excel.Cells[i + 2, 8]).NumberFormat = "yyyy-m-d";
//设置身份证号的格式
excel.get_Range(excel.Cells[2, 10], excel.Cells[i + 2, 10]).NumberFormatLocal = "0";
//设置Excel水平对齐方式
excel.get_Range(excel.Cells[1, 1], excel.Cells[i + 2, j + 2]).HorizontalAlignment
= XlHAlign.xlHAlignLeft;
//显示当前窗口
excel.Visible = true;
}
glassy2000 2012-04-04
  • 打赏
  • 举报
回复
对不起打错了!

正确的是:导出的EXCEL在excel2007下打开出错

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧