GridView 导入excel(2007版的格式)

u010703853 2014-05-08 10:21:48
我之前写的是导入2003的excel可以,可是用2007的不能打开,总是报错,而且内容是乱码
下面是我下载excel部分的代码

protected void Button1_Click(object sender, EventArgs e)
{
string XX = Label1.Text + Label3.Text;
Export("application/ms-excel", XX + "月份采购计划.xls");
}


public override void VerifyRenderingInServerForm(Control control)
{

}

private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}

...全文
293 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 楼主 u010703853 的回复:
我之前写的是导入2003的excel可以,可是用2007的不能打开,总是报错,而且内容是乱码 下面是我下载excel部分的代码

  protected void Button1_Click(object sender, EventArgs e)
        {
            string XX = Label1.Text + Label3.Text;
            Export("application/ms-excel", XX + "月份采购计划.xls");
        }


        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        private void Export(string FileType, string FileName)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
当我把

  private void Export(string FileType, string FileName)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
//之前是UTF7   现在改为了utf8 改过之后 ,它可以打开,问题是不是修改,一修改之后,在打开就是乱码了!!!!!
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
欢乐的小猪 2014-05-08
  • 打赏
  • 举报
回复
据我所知,某些页面上的“导出到excel”使用的某些控件存在这样的问题,比如对excel2003及以前的版本可能使用下面的方法: RadGrid.MasterTableView.ExportToExcel() 而对excel 2007可能使用另一种不同的方法: RadGrid.MasterTableView.ExportToExcel2007()
  • 打赏
  • 举报
回复
Teng_s2000 2014-05-08
  • 打赏
  • 举报
回复
为啥要用UTF-7 encoding呢? Excel2003和Excel2007的格式不一样,试试这个: Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
h733y 2014-05-08
  • 打赏
  • 举报
回复
引用 9 楼 u010703853 的回复:
[quote=引用 8 楼 h733y 的回复:] 你这用的是字节I/O流的方式。给你个完整的例子,请参考: private void btn_Export_Click(object sender, EventArgs e) { #region SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.Title = "Export Microsoft.Office.Interop.Excel File"; saveFileDialog.ShowDialog(); if (saveFileDialog.FileName == "") return; Stream myStream; myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); string str = ""; try { for (int i = 0; i < dgvSaleHistory.ColumnCount; i++) { if (i > 0) { str += "\t"; } str += dgvSaleHistory.Columns[i].HeaderText; } sw.WriteLine(str); for (int j = 0; j < dgvSaleHistory.Rows.Count; j++) { string tempStr = ""; for (int k = 0; k < dgvSaleHistory.Columns.Count; k++) { if (k > 0) { tempStr += "\t"; } tempStr += dgvSaleHistory.Rows[j].Cells[k].Value.ToString(); } sw.WriteLine(tempStr); } sw.Close(); myStream.Close(); } catch (Exception ex) { MessageBox.Show("导出出错"+ex.ToString()); } finally { sw.Close(); myStream.Close(); } #endregion }
还要建类 那么麻烦呀..... 好多都不对......[/quote] SaveFileDialog是winform里面自带的一个控件,就是个保存框。你用web开发的话,改成相应的对话框就可以了,都有的。 DevSaveHistory是我一个datagridview的名称,你换成你自己的datagridview就可以了。其它的没什么问题呀,套用也是稍微要改下的。
xian_wwq 2014-05-08
  • 打赏
  • 举报
回复
引用 7 楼 u010703853 的回复:
[quote=引用 6 楼 xian_wwq 的回复:] 有个办法,你用文本文件打开csv看是不是乱码, 如果是,那就是存的有问题,如果不是, 那就是excel的格式设置问题。
用文本文件打开csv看是乱码! 可问题是保存的代码怎么写 有代码没有?我搞不定呀....[/quote]


 StreamWriter sw = new StreamWriter(filePath, false, Encoding.Default);
 ...
 {
      //write  sth
      sw.WriteLine(“someting... ”);
 }
  sw.Flush();
  sw.Close();
  sw.Dispose();
  • 打赏
  • 举报
回复
引用 9 楼 u010703853 的回复:
[quote=引用 8 楼 h733y 的回复:] 你这用的是字节I/O流的方式。给你个完整的例子,请参考: private void btn_Export_Click(object sender, EventArgs e) { #region SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.Title = "Export Microsoft.Office.Interop.Excel File"; saveFileDialog.ShowDialog(); if (saveFileDialog.FileName == "") return; Stream myStream; myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); string str = ""; try { for (int i = 0; i < dgvSaleHistory.ColumnCount; i++) { if (i > 0) { str += "\t"; } str += dgvSaleHistory.Columns[i].HeaderText; } sw.WriteLine(str); for (int j = 0; j < dgvSaleHistory.Rows.Count; j++) { string tempStr = ""; for (int k = 0; k < dgvSaleHistory.Columns.Count; k++) { if (k > 0) { tempStr += "\t"; } tempStr += dgvSaleHistory.Rows[j].Cells[k].Value.ToString(); } sw.WriteLine(tempStr); } sw.Close(); myStream.Close(); } catch (Exception ex) { MessageBox.Show("导出出错"+ex.ToString()); } finally { sw.Close(); myStream.Close(); } #endregion }
还要建类 那么麻烦呀..... 好多都不对......[/quote]有写右键解析就好了呀
  • 打赏
  • 举报
回复

引用 8 楼 h733y 的回复:
你这用的是字节I/O流的方式。给你个完整的例子,请参考:
private void btn_Export_Click(object sender, EventArgs e)
{
#region
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "Export Microsoft.Office.Interop.Excel File";
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName == "")
return;
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));

string str = "";
try
{
for (int i = 0; i < dgvSaleHistory.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += dgvSaleHistory.Columns[i].HeaderText;
}
sw.WriteLine(str);
for (int j = 0; j < dgvSaleHistory.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < dgvSaleHistory.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dgvSaleHistory.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}

catch (Exception ex)
{
MessageBox.Show("导出出错"+ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
#endregion
}



还要建类 那么麻烦呀.....
好多都不对......
h733y 2014-05-08
  • 打赏
  • 举报
回复
你这用的是字节I/O流的方式。给你个完整的例子,请参考: private void btn_Export_Click(object sender, EventArgs e) { #region SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.Title = "Export Microsoft.Office.Interop.Excel File"; saveFileDialog.ShowDialog(); if (saveFileDialog.FileName == "") return; Stream myStream; myStream = saveFileDialog.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); string str = ""; try { for (int i = 0; i < dgvSaleHistory.ColumnCount; i++) { if (i > 0) { str += "\t"; } str += dgvSaleHistory.Columns[i].HeaderText; } sw.WriteLine(str); for (int j = 0; j < dgvSaleHistory.Rows.Count; j++) { string tempStr = ""; for (int k = 0; k < dgvSaleHistory.Columns.Count; k++) { if (k > 0) { tempStr += "\t"; } tempStr += dgvSaleHistory.Rows[j].Cells[k].Value.ToString(); } sw.WriteLine(tempStr); } sw.Close(); myStream.Close(); } catch (Exception ex) { MessageBox.Show("导出出错"+ex.ToString()); } finally { sw.Close(); myStream.Close(); } #endregion }
  • 打赏
  • 举报
回复
引用 6 楼 xian_wwq 的回复:
有个办法,你用文本文件打开csv看是不是乱码, 如果是,那就是存的有问题,如果不是, 那就是excel的格式设置问题。
用文本文件打开csv看是乱码! 可问题是保存的代码怎么写 有代码没有?我搞不定呀....
xian_wwq 2014-05-08
  • 打赏
  • 举报
回复
有个办法,你用文本文件打开csv看是不是乱码, 如果是,那就是存的有问题,如果不是, 那就是excel的格式设置问题。
  • 打赏
  • 举报
回复
引用 4 楼 xian_wwq 的回复:
如果没有强制性要求,干脆导成csv文件,兼容性好
按照你的说法,我把xls格式 改为 csv 了 可是 虽然打开没有报错,可是都是乱码..... 求 代码。。。。。。。
xian_wwq 2014-05-08
  • 打赏
  • 举报
回复
如果没有强制性要求,干脆导成csv文件,兼容性好

110,544

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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