C#winform中如何把表导出到EXCEL
C#winform中如何把表导出到EXCEL使用的是datagridview控件,把其中的表导入到一个新的EXCEL文件中。是不是把datagridview中表遍历一遍,...
C#winform中如何把表导出到EXCEL 使用的是datagridview控件,把其中的表导入到一个新的EXCEL文件中。是不是把datagridview中表遍历一遍,一个个写到EXCEL文件中?具体怎么写高手指点下,我用的ACCESS数据库。最好给个例子看看~
展开
3个回答
2013-09-09
展开全部
我们一般用的方法是将表遍历一遍,然后写入到excel中,主要代码如下: /// <summary>
/// 指定文件名称、表名、DataGridView导出DataGridView中的数据到Excel中
/// </summary>
/// <param name="location">输出文件的位置</param>
/// <param name="tableName">表名</param>
/// <param name="dt">DataGridView名称</param>
public void GridViewToExcel(string location, string tableName, DataGridView dt)
{
Stream myStream;
myStream = File.Open(location,FileMode.Create);
//StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
int m=0;
try
{
//写标题
for (int i = 0; i < dt.ColumnCount; i++)
{
if (dt.Columns[i].Visible == false)
{
continue;
}
else
{
str += dt.Columns[i].HeaderText + "\t";
}
} //string aa = str.Substring(0, str.Length - 1);
sw.WriteLine(str.Substring(0,str.Length-1)); //写内容
for (int j = 0; j < dt.Rows.Count; j++)
{
if (dt.Rows[j].Visible == true)
{
string tempStr = "";
for (int k = 0; k < dt.Columns.Count; k++)
{
if (dt.Columns[k].Visible == false)
{
continue;
}
else
{
tempStr += dt.Rows[j].Cells[k].Value.ToString() + "\t";
}
} sw.WriteLine(tempStr.Substring(0,tempStr.Length-1));
}
}
sw.Close();
myStream.Close();
} catch (Exception e)
{
MessageBox.Show(e.ToString());
} finally
{
sw.Close();
myStream.Close();
}
}
/// 指定文件名称、表名、DataGridView导出DataGridView中的数据到Excel中
/// </summary>
/// <param name="location">输出文件的位置</param>
/// <param name="tableName">表名</param>
/// <param name="dt">DataGridView名称</param>
public void GridViewToExcel(string location, string tableName, DataGridView dt)
{
Stream myStream;
myStream = File.Open(location,FileMode.Create);
//StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
int m=0;
try
{
//写标题
for (int i = 0; i < dt.ColumnCount; i++)
{
if (dt.Columns[i].Visible == false)
{
continue;
}
else
{
str += dt.Columns[i].HeaderText + "\t";
}
} //string aa = str.Substring(0, str.Length - 1);
sw.WriteLine(str.Substring(0,str.Length-1)); //写内容
for (int j = 0; j < dt.Rows.Count; j++)
{
if (dt.Rows[j].Visible == true)
{
string tempStr = "";
for (int k = 0; k < dt.Columns.Count; k++)
{
if (dt.Columns[k].Visible == false)
{
continue;
}
else
{
tempStr += dt.Rows[j].Cells[k].Value.ToString() + "\t";
}
} sw.WriteLine(tempStr.Substring(0,tempStr.Length-1));
}
}
sw.Close();
myStream.Close();
} catch (Exception e)
{
MessageBox.Show(e.ToString());
} finally
{
sw.Close();
myStream.Close();
}
}
2013-09-09
展开全部
/// <summary>
/// 定义导出Excel的函数
/// </summary>
/// <param name="FileType">到处的类型</param>
/// <param name="FileName">路径名</param>
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.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);
datagridview1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
/// <summary>
/// 此方法必重写,否则会出错
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
/// 定义导出Excel的函数
/// </summary>
/// <param name="FileType">到处的类型</param>
/// <param name="FileName">路径名</param>
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.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);
datagridview1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
/// <summary>
/// 此方法必重写,否则会出错
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-09
展开全部
我这里有个很简单的代码你一看就会了,其实就是一个写文件的代码哈首先你拖放一个savefiledialog哈!取名叫savaexcel. savaexcel.Filter = "Excel文件(*.xls)|*.xls|Word文件(*.doc)|*.doc";
if (savaexcel.ShowDialog() == DialogResult.OK)
{
string filename = savaexcel.FileName;
string str = string.Empty;
for (int i = 0; i <datagridview1.Columns.Count ; i++)
{
str += datagridview1.columns[i].Headtext+ "\t";//写标题
if (i == datagridview1.Columns.Count - 1)
{
str += "\r";
} }
for (int i = 0; i < datagridview1.rows.Count; i++)
{
for (int j = 0; j <=datagridview1.Columns.Count ; j++)
{
str += "'" +datagridview1.rows[i].cells[j].value.tostring()+ "\t"; if (j ==datagridview1.Columns.Count - 1)
{
str += "\r";
}
}
}
StreamWriter sw = new StreamWriter(filename, true, Encoding.GetEncoding("gb2312"));
sw.Write(str);
sw.Close(); } 记得引入system.io空间 这是我改过来的,有问题可以联系我哈!不知道有没有bug,你试一下哈!我原来用的listview。不知道对没有。应该没有错哈!
if (savaexcel.ShowDialog() == DialogResult.OK)
{
string filename = savaexcel.FileName;
string str = string.Empty;
for (int i = 0; i <datagridview1.Columns.Count ; i++)
{
str += datagridview1.columns[i].Headtext+ "\t";//写标题
if (i == datagridview1.Columns.Count - 1)
{
str += "\r";
} }
for (int i = 0; i < datagridview1.rows.Count; i++)
{
for (int j = 0; j <=datagridview1.Columns.Count ; j++)
{
str += "'" +datagridview1.rows[i].cells[j].value.tostring()+ "\t"; if (j ==datagridview1.Columns.Count - 1)
{
str += "\r";
}
}
}
StreamWriter sw = new StreamWriter(filename, true, Encoding.GetEncoding("gb2312"));
sw.Write(str);
sw.Close(); } 记得引入system.io空间 这是我改过来的,有问题可以联系我哈!不知道有没有bug,你试一下哈!我原来用的listview。不知道对没有。应该没有错哈!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询