一个关于Excel的处理类(部分参考网络上)
2008-03-17 12:46
951 查看
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
namespace Northwind.Web.common
{
public class ExcelHelper
{
//将控件转换为Excel表
public static void ExportToExcel(Control c, string fileName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-7";//不行就改为utf-7
HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.xls", fileName));
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");//设置输出流为简体中文
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
c.RenderControl(oHtmlTextWriter);
HttpContext.Current.Response.Write(oStringWriter.ToString());
HttpContext.Current.Response.End();
}
//判断有没有相同的列,暂时只支持一列
public static bool CanImport(string filename, string sheetName, string column)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0;";
if (column != "")
{
using (OleDbConnection conn = new OleDbConnection(strConn))
{
OleDbDataAdapter oada = new OleDbDataAdapter(string.Format("select {0} from [{1}$] group by {0} having count({0})=2", column, sheetName), strConn);
DataTable dt = new DataTable();
oada.Fill(dt);
conn.Close();
if (dt.Rows.Count > 0) { return false; }
}
}
return true;
}
//将excel数据转换为DataTable
public static DataTable ImportToTable(string filename, string sheetName)
{
DataTable dt = new DataTable();
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0;";
using (OleDbConnection conn = new OleDbConnection(strConn))
{
OleDbDataAdapter oada = new OleDbDataAdapter(string.Format("select * from [{0}$]", sheetName), strConn);
oada.Fill(dt);
}
return dt;
}
//从gridview导出Excel
public static void ExportFromGridview(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "utf-7";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");//设置输出流为简体中文
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}.xls", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a form to contain the grid
Table table = new Table();
// add the header row to the table
if (gv.HeaderRow != null)
{
PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
PrepareControlForExport(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gv.FooterRow != null)
{
PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
//处理gridview里面的特殊控件,如下拉列表或文本框等
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
if (current.HasControls())
{
PrepareControlForExport(current);
}
}
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
namespace Northwind.Web.common
{
public class ExcelHelper
{
//将控件转换为Excel表
public static void ExportToExcel(Control c, string fileName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-7";//不行就改为utf-7
HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.xls", fileName));
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");//设置输出流为简体中文
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
c.RenderControl(oHtmlTextWriter);
HttpContext.Current.Response.Write(oStringWriter.ToString());
HttpContext.Current.Response.End();
}
//判断有没有相同的列,暂时只支持一列
public static bool CanImport(string filename, string sheetName, string column)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0;";
if (column != "")
{
using (OleDbConnection conn = new OleDbConnection(strConn))
{
OleDbDataAdapter oada = new OleDbDataAdapter(string.Format("select {0} from [{1}$] group by {0} having count({0})=2", column, sheetName), strConn);
DataTable dt = new DataTable();
oada.Fill(dt);
conn.Close();
if (dt.Rows.Count > 0) { return false; }
}
}
return true;
}
//将excel数据转换为DataTable
public static DataTable ImportToTable(string filename, string sheetName)
{
DataTable dt = new DataTable();
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0;";
using (OleDbConnection conn = new OleDbConnection(strConn))
{
OleDbDataAdapter oada = new OleDbDataAdapter(string.Format("select * from [{0}$]", sheetName), strConn);
oada.Fill(dt);
}
return dt;
}
//从gridview导出Excel
public static void ExportFromGridview(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "utf-7";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");//设置输出流为简体中文
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}.xls", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a form to contain the grid
Table table = new Table();
// add the header row to the table
if (gv.HeaderRow != null)
{
PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
PrepareControlForExport(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gv.FooterRow != null)
{
PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
// render the table into the htmlwriter
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
//处理gridview里面的特殊控件,如下拉列表或文本框等
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
if (current.HasControls())
{
PrepareControlForExport(current);
}
}
}
}
}
相关文章推荐
- 关于Python操作excel过程中,将多个内容写到同一个excel的不同sheet页的处理
- 关于Excel操作编写的一个软件设计构思案例[连载] --如何把处理好后的数据导出Excel文件中(含背景\字体颜色设置)
- 一个关于OutOfMemoryError的处理【转载】谢谢网络上大神们
- 关于用Java实现发送邮件(部分代码参考网络来源)
- 关于JSP显示Excel图表的JFreeChart使用(参考网络资料)
- OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版。 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R
- 一个关于php使用pdo方式进行数据库连接和处理的类
- 声明:关于该博客部分Java等方向知识参考来源的说明
- awk与sed:一个关于多行处理的例子
- 遇见的又一个新问题,关于显示文章条目的时候,显示宽度的处理
- VB 2010中Excel文件处理的一个奇怪问题
- 关于 数据源 导出excel (这是) 通过 画一个html 实现的、最简单、好理解、的代码、
- 大数问题 加减乘除余 有参考部分网络代码如除法和减。其余原创。
- 关于一个音频播放器在工作时,打开另一个播放器或者进来电话之类的处理
- 关于poi解析word和excel的问题,解析word中的table,获取部分table的行数和列数不正确
- 关于如何处理ubuntu16.04无法连接网络问题
- 一个我很长时间才解决的关于xp与2000共享的网络问题
- 求教一个android的关于图片处理的问题?
- 又是一个关于EXCEL文件导入mdb后台的问题
- 今天发现一个Java的关于java.io.File在处理com1到com9为文件名的Bug.