最近做了个导出Excel的程序! 那个心烦啊。
开始要求是直接将gridview导出成Excel,简单!
这样就可以解决了! 但要重写个 方法 才能导出
private void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
{
// Confirms that an HtmlForm control is rendered for
}
后来要求又改了,往导出的Excel里 加东西 并不只是哪个 gridview里德值! 棘手.....
然后想到了 往 oStringWriter 里 加入 html 代码来加值 。 比如划table啊、 来控制
比如 :
ChanpDataGrid.AllowPaging = false; //清除分页
ChanpDataGrid.AllowSorting = false; //清除排序
DataTable dtChanp = (DataTable)Session["SendChanpDt"];
ChanpDataGrid.Columns.RemoveAt(5); //清除操作列
ChanpDataGrid.DataSource = dtChanp;
ChanpDataGrid.DataBind();
ChanpDataGrid.AllowSorting = false; //清除排序
DataTable dtChanp = (DataTable)Session["SendChanpDt"];
ChanpDataGrid.Columns.RemoveAt(5); //清除操作列
ChanpDataGrid.DataSource = dtChanp;
ChanpDataGrid.DataBind();
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=SendInfo.xls");
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
oStringWriter.Write("片区:"); oStringWriter.Write(areaDDList.SelectedItem.Text + "<br />");
oStringWriter.Write("开票单位:"); oStringWriter.Write(Kai_DW.SelectedItem.Text + " "); oStringWriter.Write("收货单位:"); oStringWriter.Write(ShuoDW.Text + "<br />");
oStringWriter.Write("申请单位:"); oStringWriter.Write(ReqDW.Text + " "); oStringWriter.Write("申请人姓名:"); oStringWriter.Write(Req_Name.Text + "<br />");
oStringWriter.Write("交货地点:"); oStringWriter.Write("从 " + DZStart.Text + " 到 " + DZEnd.Text + " "); oStringWriter.Write("申请发货时间:"); oStringWriter.Write(Req_SendDate.Text + "<br />");
oStringWriter.Write("联系人:" + Link_Name.Text + " "); oStringWriter.Write("联系人电话:" + Link_Tel.Text + " " + "联系人传真:" + Link_Fax.Text + "<br />");
oStringWriter.Write("销售公司营销管理处::"); oStringWriter.Write(XSMana.Text + "<br />");
oStringWriter.Write("发货方式:"); oStringWriter.Write(Send_Type.SelectedItem.Text + " "); oStringWriter.Write("传递时间:"); oStringWriter.Write(CD_Date.Text + "<br />");
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=SendInfo.xls");
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
oStringWriter.Write("片区:"); oStringWriter.Write(areaDDList.SelectedItem.Text + "<br />");
oStringWriter.Write("开票单位:"); oStringWriter.Write(Kai_DW.SelectedItem.Text + " "); oStringWriter.Write("收货单位:"); oStringWriter.Write(ShuoDW.Text + "<br />");
oStringWriter.Write("申请单位:"); oStringWriter.Write(ReqDW.Text + " "); oStringWriter.Write("申请人姓名:"); oStringWriter.Write(Req_Name.Text + "<br />");
oStringWriter.Write("交货地点:"); oStringWriter.Write("从 " + DZStart.Text + " 到 " + DZEnd.Text + " "); oStringWriter.Write("申请发货时间:"); oStringWriter.Write(Req_SendDate.Text + "<br />");
oStringWriter.Write("联系人:" + Link_Name.Text + " "); oStringWriter.Write("联系人电话:" + Link_Tel.Text + " " + "联系人传真:" + Link_Fax.Text + "<br />");
oStringWriter.Write("销售公司营销管理处::"); oStringWriter.Write(XSMana.Text + "<br />");
oStringWriter.Write("发货方式:"); oStringWriter.Write(Send_Type.SelectedItem.Text + " "); oStringWriter.Write("传递时间:"); oStringWriter.Write(CD_Date.Text + "<br />");
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.ChanpDataGrid.RenderControl(oHtmlTextWriter);
string str = oHtmlTextWriter.ToString();
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
this.ChanpDataGrid.RenderControl(oHtmlTextWriter);
string str = oHtmlTextWriter.ToString();
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
这样就比较 SB 。 呵呵~
妈的后来要求又变了,要直接显示打印预览! 这个头疼~ 只有用到了 Excel.dll 来搞了!
首先要完整的安装Excel。 才能进行如下配置。
这个dll 很强大, 针对 Excel的操作。 里面都包含有方法!
要用这个东西 首先得配置下电脑的 Excel操作权限 。 在开始-->运行 里输入 dcomcnfg 到里面设置。
服务器端必须要设置的一点是COM组件的操作权限。在命令行键入“DCOMCNFG”,则进入COM组件配置界面,选择Microsoft Excel后点击属性,将三个单选项一律选择自定义,编辑中将Everyone加入所有权限。在“标识”中设置为交互式用户,保存完毕后重新启动服务器。
***如果不设置交互式用户,EXCEL进程如果停留在内存中,你是无法结束进程的
***如果不设置交互式用户,EXCEL进程如果停留在内存中,你是无法结束进程的
具体Google!
这样B/S 程序就可以操作本机Excel了 。
创建如下:
ZYP.DBUtility.ExcelHelper excel = new ZYP.DBUtility.ExcelHelper();
excel.CreateExcel();
excel.CreateExcel();
Excel.Worksheet sheet = (Excel.Worksheet)excel.myWorkBook.Worksheets[1];
sheet.Visible = Excel.XlSheetVisibility.xlSheetVisible;
excel.myExcel.Caption = "发货信息";
sheet.Activate();
excel.myExcel.Caption = "发货信息";
sheet.Activate();
这个是创建个 Sheet .
给单元格样式:
excel.SetColumnWidth(1, 1, 9); //编号
excel.SetColumnWidth(3, 3, 20); //产品型号
excel.SetColumnWidth(2, 2, 12); //物料码
excel.SetColumnWidth(4, 4, 8); //订货数量
excel.SetColumnWidth(6, 6, 15); //产品名称
excel.SetColumnWidth(7, 7, 25); //特殊说明
excel.SetColumnWidth(5, 5, 8); //确认数量
excel.CellAutoWrap(5, 7, (dt.Rows.Count + 12), 7); //设置自动换行
excel.CellsAlignment(4, 1, 4, 7, ZYP.DBUtility.ExcelHelper.ExcelHAlign.居中, ZYP.DBUtility.ExcelHelper.ExcelVAlign.居中); //明细标题居中
//excel.CellsAlignment(8, 3, 8, 3, ZYP.DBUtility.ExcelHelper.ExcelHAlign.靠右, ZYP.DBUtility.ExcelHelper.ExcelVAlign.居中); //合计的剧右
//excel.CellsAlignment(8, 3, 8, 3, ZYP.DBUtility.ExcelHelper.ExcelHAlign.靠右, ZYP.DBUtility.ExcelHelper.ExcelVAlign.居中); //"合计"的剧右
excel.CellsAlignment((dt.Rows.Count + 8), 7, (dt.Rows.Count + 12), 7, ZYP.DBUtility.ExcelHelper.ExcelHAlign.靠左, ZYP.DBUtility.ExcelHelper.ExcelVAlign.居中); //时间居左
excel.FontColor(4, 1, 4, 7, ZYP.DBUtility.ExcelHelper.ColorIndex.梅红);
excel.FontStyle(4, 1, 4, 7, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //明细表头加粗
excel.FontStyle(3, 2, 3, 2, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //三行加粗
excel.FontStyle(2, 1, 2, 1, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //二行加粗
excel.FontStyle(1, 1, 1, 7, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //一行加粗
excel.FontStyle(dt.Rows.Count + 12, 1, dt.Rows.Count + 12, 7, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //主管签字加粗
excel.FontStyle(dt.Rows.Count + 4, 1, dt.Rows.Count + 4, 7, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //合计加粗
//excel.CellsBackColor(4, 1, 4, 7, ZYP.DBUtility.ExcelHelper.ColorIndex.白色, ZYP.DBUtility.ExcelHelper.Pattern.Horizontal); //明细表头背景
excel.CellsUnite((dt.Rows.Count + 5), 2, (dt.Rows.Count + 5),7); //备注 合并
//excel.CellsUnite(4, 6, 4, 7); //产品名称 合并
excel.SetColumnWidth(3, 3, 20); //产品型号
excel.SetColumnWidth(2, 2, 12); //物料码
excel.SetColumnWidth(4, 4, 8); //订货数量
excel.SetColumnWidth(6, 6, 15); //产品名称
excel.SetColumnWidth(7, 7, 25); //特殊说明
excel.SetColumnWidth(5, 5, 8); //确认数量
excel.CellAutoWrap(5, 7, (dt.Rows.Count + 12), 7); //设置自动换行
excel.CellsAlignment(4, 1, 4, 7, ZYP.DBUtility.ExcelHelper.ExcelHAlign.居中, ZYP.DBUtility.ExcelHelper.ExcelVAlign.居中); //明细标题居中
//excel.CellsAlignment(8, 3, 8, 3, ZYP.DBUtility.ExcelHelper.ExcelHAlign.靠右, ZYP.DBUtility.ExcelHelper.ExcelVAlign.居中); //合计的剧右
//excel.CellsAlignment(8, 3, 8, 3, ZYP.DBUtility.ExcelHelper.ExcelHAlign.靠右, ZYP.DBUtility.ExcelHelper.ExcelVAlign.居中); //"合计"的剧右
excel.CellsAlignment((dt.Rows.Count + 8), 7, (dt.Rows.Count + 12), 7, ZYP.DBUtility.ExcelHelper.ExcelHAlign.靠左, ZYP.DBUtility.ExcelHelper.ExcelVAlign.居中); //时间居左
excel.FontColor(4, 1, 4, 7, ZYP.DBUtility.ExcelHelper.ColorIndex.梅红);
excel.FontStyle(4, 1, 4, 7, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //明细表头加粗
excel.FontStyle(3, 2, 3, 2, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //三行加粗
excel.FontStyle(2, 1, 2, 1, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //二行加粗
excel.FontStyle(1, 1, 1, 7, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //一行加粗
excel.FontStyle(dt.Rows.Count + 12, 1, dt.Rows.Count + 12, 7, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //主管签字加粗
excel.FontStyle(dt.Rows.Count + 4, 1, dt.Rows.Count + 4, 7, true, false, ZYP.DBUtility.ExcelHelper.UnderlineStyle.无下划线); //合计加粗
//excel.CellsBackColor(4, 1, 4, 7, ZYP.DBUtility.ExcelHelper.ColorIndex.白色, ZYP.DBUtility.ExcelHelper.Pattern.Horizontal); //明细表头背景
excel.CellsUnite((dt.Rows.Count + 5), 2, (dt.Rows.Count + 5),7); //备注 合并
//excel.CellsUnite(4, 6, 4, 7); //产品名称 合并
给sheet 赋值 :
sheet.Cells[1, 4] = "发 货 通 知";
sheet.Cells[2, 1] = "生产处营销管理室:";
sheet.Cells[3, 2] = "请协助办理以下商品发运。";
//发货明细
sheet.Cells[4, 1] = "序号";
sheet.Cells[4, 2] = "物料码";
sheet.Cells[4, 3] = "产品型号";
sheet.Cells[4, 4] = "订货数量";
sheet.Cells[4, 5] = "确认数量";
sheet.Cells[4, 6] = "产品名称";
sheet.Cells[2, 1] = "生产处营销管理室:";
sheet.Cells[3, 2] = "请协助办理以下商品发运。";
//发货明细
sheet.Cells[4, 1] = "序号";
sheet.Cells[4, 2] = "物料码";
sheet.Cells[4, 3] = "产品型号";
sheet.Cells[4, 4] = "订货数量";
sheet.Cells[4, 5] = "确认数量";
sheet.Cells[4, 6] = "产品名称";
具体的还得看你的要求
//excel.myExcel.Visible = true;
sheet.PageSetup.PrintGridlines = false; //去掉打印时的网格线
//sheet.PrintPreview(true);
sheet.PageSetup.PrintGridlines = false; //去掉打印时的网格线
//sheet.PrintPreview(true);
这是 最后显示打印是的设置
在文件的导出过程中要 用相对路径。 要不然Excel导出后打开 会出现 加载是问题(找不到../.../.../style.css) 这个问题。
项目中使用Excell.dll组件操作excel文件,在VS 2005调试的时候一切正常,但以部署到服务器上就出现了以下错误:
System.UnauthorizedAccessException: 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。
在 ExcelHelper.StartExcel() 位置 d:DotNetworkspaceWebSitesScoreCardApp_CodeExcelHelper.cs:行号 111
在 ExcelHelper..ctor(Boolean visible) 位置 d:DotNetworkspaceWebSitesScoreCardApp_CodeExcelHelper.cs:行号 101
在 ScoreCard.Process(String country) 位置 d:DotNetworkspaceWebSitesScoreCardApp_CodeScoreCard.cs:行号 109
Google 一下,也找到了一些方案,但均不凑效,后来经过尝试,找到了如下的 解决方案:
在 ExcelHelper.StartExcel() 位置 d:DotNetworkspaceWebSitesScoreCardApp_CodeExcelHelper.cs:行号 111
在 ExcelHelper..ctor(Boolean visible) 位置 d:DotNetworkspaceWebSitesScoreCardApp_CodeExcelHelper.cs:行号 101
在 ScoreCard.Process(String country) 位置 d:DotNetworkspaceWebSitesScoreCardApp_CodeScoreCard.cs:行号 109
Google 一下,也找到了一些方案,但均不凑效,后来经过尝试,找到了如下的 解决方案:
在Web.config文件中的 system.web节 中加入
<identity impersonate="true" userName="JAMES/Administrator" password="##################" />
userName 的格式为:{机器名}/{用户名} password 为登录密码
userName 的格式为:{机器名}/{用户名} password 为登录密码
string tableName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0][2].ToString();