方式一:用流的方式向EXCEL中写如数据
(1)通过web页面的方式
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
///
<summary>
/// 导出
/// </summary>
/// <returns> 要导出的内容 </returns>
public string Export()
{
StringBuilder table = new StringBuilder();
table.Append( @" <b>项目组开模</b><table class='table'><tr><td class='td_center' colspan='2'>项目组</td><td class='td_center' colspan='2'>OA</td><td class='td_center'>编写人</td>
<td class='td_center' colspan='2'>周大有</td></tr> " );
table.Append( " <tr><td class='td_center'>序号</td> " );
table.Append( " <td class='td_center'>产品型号(完整)</td> " );
table.Append( " <td class='td_center'>营业员/营业办</td> " );
table.Append( " <td class='td_center'>终端客户/方案公司或代理</td> " );
table.Append( " <td class='td_center'>用途</td> " );
table.Append( " <td class='td_center'>产品信息</td> " );
table.Append( " <td class='td_center'>事业部</td> " );
table.Append( " </tr> " );
table.Append( " </table> " );
return table.ToString();
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="page"> 操作页面 </param>
/// <param name="fileName"> 导出的文件名称 </param>
/// <param name="text"> 要导出的内容 </param>
public void ExportExcel(Page page, string fileName, string text)
{
page.EnableViewState = false ;
try
{
page.Response.ClearContent();
page.Response.Buffer = true ;
page.Response.Charset = " GB2312 " ; // 设置字符集,解决中文乱码问题
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding( " GB2312 " );
page.Response.Write( " <meta http-equiv=Content-Type content=\"text/html;charset=GB2312\"> " ); // 解决乱码问题
// 解决HTTP头中文乱码问题
string strExcelText = DateTime.Now.ToShortDateString() + " \t " + fileName; // Excel显示的内容
string strEncode = System.Web.HttpUtility.UrlEncode(strExcelText, System.Text.Encoding.UTF8); // 进行编码的格式,用gb2312出错
page.Response.AddHeader( " content-disposition " , " attachment;filename=\" " + strEncode + " .xls\" " ); // 对保存标题进行编码
page.Response.ContentType = " application/vnd.xls " ; // 设置输出格式
// 设置样式
page.Response.Write( @" <html><head><style>.table{border: 1px solid #000000;padding:0;margin:0 auto;border-width: thin;border-collapse: collapse;}
.td_head{border: 1px solid #000000;border-width: thin;text-align:center;font-size:12px;padding: 3px 3px 3px 8px;background: #000000;}
.td_left{border: 1px solid #000000;font-size:12px;border-width: thin;text-align:left;padding: 3px 3px 3px 8px;mso-number-format:\@;}
.td_left_number{border: 1px solid #000000;font-size:12px;border-width: thin;text-align:left;padding: 3px 3px 3px 8px;}
.td_center{border: 1px solid #000000;font-size:12px;border-width: thin;text-align:center;padding: 3px 3px 3px 8px;mso-number-format:\@;}
.td_center_number{border: 1px solid #7777cc;font-size:12px;border-width: thin;text-align:center;padding: 3px 3px 3px 8px;}
.td_right{border: 1px solid #000000;font-size:12px;border-width: thin;text-align:right;padding: 3px 3px 3px 8px;mso-number-format:\@;}
.td_right_number{border: 1px solid #000000;font-size:12px;border-width: thin;text-align:right;padding: 3px 3px 3px 8px;}
</style></head><body> " );
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
htw.WriteLine(text); // 将数据输出
page.Response.Write(sw.ToString());
page.Response.Write( " </body></html> " );
page.Response.Flush();
page.Response.End();
}
catch
{
return ;
}
finally
{
// 恢复原来控件内容
page.EnableViewState = true ;
}
}
protected void Button1_Click1( object sender, EventArgs e)
{
string strText = Export();
strText = this .AlarmInfo.InnerHtml;
ExportExcel( this , " 测试表 " , strText);
}
/// 导出
/// </summary>
/// <returns> 要导出的内容 </returns>
public string Export()
{
StringBuilder table = new StringBuilder();
table.Append( @" <b>项目组开模</b><table class='table'><tr><td class='td_center' colspan='2'>项目组</td><td class='td_center' colspan='2'>OA</td><td class='td_center'>编写人</td>
<td class='td_center' colspan='2'>周大有</td></tr> " );
table.Append( " <tr><td class='td_center'>序号</td> " );
table.Append( " <td class='td_center'>产品型号(完整)</td> " );
table.Append( " <td class='td_center'>营业员/营业办</td> " );
table.Append( " <td class='td_center'>终端客户/方案公司或代理</td> " );
table.Append( " <td class='td_center'>用途</td> " );
table.Append( " <td class='td_center'>产品信息</td> " );
table.Append( " <td class='td_center'>事业部</td> " );
table.Append( " </tr> " );
table.Append( " </table> " );
return table.ToString();
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="page"> 操作页面 </param>
/// <param name="fileName"> 导出的文件名称 </param>
/// <param name="text"> 要导出的内容 </param>
public void ExportExcel(Page page, string fileName, string text)
{
page.EnableViewState = false ;
try
{
page.Response.ClearContent();
page.Response.Buffer = true ;
page.Response.Charset = " GB2312 " ; // 设置字符集,解决中文乱码问题
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding( " GB2312 " );
page.Response.Write( " <meta http-equiv=Content-Type content=\"text/html;charset=GB2312\"> " ); // 解决乱码问题
// 解决HTTP头中文乱码问题
string strExcelText = DateTime.Now.ToShortDateString() + " \t " + fileName; // Excel显示的内容
string strEncode = System.Web.HttpUtility.UrlEncode(strExcelText, System.Text.Encoding.UTF8); // 进行编码的格式,用gb2312出错
page.Response.AddHeader( " content-disposition " , " attachment;filename=\" " + strEncode + " .xls\" " ); // 对保存标题进行编码
page.Response.ContentType = " application/vnd.xls " ; // 设置输出格式
// 设置样式
page.Response.Write( @" <html><head><style>.table{border: 1px solid #000000;padding:0;margin:0 auto;border-width: thin;border-collapse: collapse;}
.td_head{border: 1px solid #000000;border-width: thin;text-align:center;font-size:12px;padding: 3px 3px 3px 8px;background: #000000;}
.td_left{border: 1px solid #000000;font-size:12px;border-width: thin;text-align:left;padding: 3px 3px 3px 8px;mso-number-format:\@;}
.td_left_number{border: 1px solid #000000;font-size:12px;border-width: thin;text-align:left;padding: 3px 3px 3px 8px;}
.td_center{border: 1px solid #000000;font-size:12px;border-width: thin;text-align:center;padding: 3px 3px 3px 8px;mso-number-format:\@;}
.td_center_number{border: 1px solid #7777cc;font-size:12px;border-width: thin;text-align:center;padding: 3px 3px 3px 8px;}
.td_right{border: 1px solid #000000;font-size:12px;border-width: thin;text-align:right;padding: 3px 3px 3px 8px;mso-number-format:\@;}
.td_right_number{border: 1px solid #000000;font-size:12px;border-width: thin;text-align:right;padding: 3px 3px 3px 8px;}
</style></head><body> " );
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
htw.WriteLine(text); // 将数据输出
page.Response.Write(sw.ToString());
page.Response.Write( " </body></html> " );
page.Response.Flush();
page.Response.End();
}
catch
{
return ;
}
finally
{
// 恢复原来控件内容
page.EnableViewState = true ;
}
}
protected void Button1_Click1( object sender, EventArgs e)
{
string strText = Export();
strText = this .AlarmInfo.InnerHtml;
ExportExcel( this , " 测试表 " , strText);
}
(2)不用web页面的方式
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
private
void
CreateExcel(DataTable db)
{
Stream myStream = new FileStream( " D:\\Excel.xls " , FileMode.Create);
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding( " gb2312 " ));
try
{
// 写内容
for ( int j = 0 ; j < db.Rows.Count; j ++ )
{
string tempStr = "" ;
for ( int k = 0 ; k < db.Columns.Count; k ++ )
{
if (k > 0 )
{
tempStr += " \t " ;
}
tempStr += db.Rows[j][k].ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
{
Stream myStream = new FileStream( " D:\\Excel.xls " , FileMode.Create);
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding( " gb2312 " ));
try
{
// 写内容
for ( int j = 0 ; j < db.Rows.Count; j ++ )
{
string tempStr = "" ;
for ( int k = 0 ; k < db.Columns.Count; k ++ )
{
if (k > 0 )
{
tempStr += " \t " ;
}
tempStr += db.Rows[j][k].ToString();
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
方式二:粘贴方式