解决方案 »
- 如何判断2个字符串中的仅仅一个字母区别
- 定时发送手机短信
- 高手推薦幾本學習.net組件/控件開的書,有電子版更好.
- vb怎么做网页更新提醒程序
- 如何通过已知控件的名称返回这个控件对象?知道用反射,但不知道如何使用。
- 编译后的DLL还会显示出源代码。
- 帮帮忙,我已经连接TCP/IP连接了服务器?但不知道怎么接收数据啊
- 在设计期间修改了OleDbCommand类的CommandText属性后, 系统会提示是否更新Parameters,请问如何用代码实现自动更新参数这一功能
- socket,web services的问题
- 谁能向我阐述一下,C#将给我们带来的美好前景
- 怎样将一个文件最后10个字节移到文件的最前面
- C#中Array类的Sort 方法与自己编写的冒泡排序算法的区别,求指教!
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();
}
如果是,那就是存的有问题,如果不是,
那就是excel的格式设置问题。
可问题是保存的代码怎么写
有代码没有?我搞不定呀....
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
}
可问题是保存的代码怎么写
有代码没有?我搞不定呀.... StreamWriter sw = new StreamWriter(filePath, false, Encoding.Default);
...
{
//write sth
sw.WriteLine(“someting... ”);
}
sw.Flush();
sw.Close();
sw.Dispose();
好多都不对......
SaveFileDialog是winform里面自带的一个控件,就是个保存框。你用web开发的话,改成相应的对话框就可以了,都有的。
DevSaveHistory是我一个datagridview的名称,你换成你自己的datagridview就可以了。其它的没什么问题呀,套用也是稍微要改下的。
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";