当前位置: 首页 > 编程日记 > 正文

.net导出到Excel与Word中(带上下标)

//输出到excel的函数,可直接copy到 cs页面
    private void OutExcel(GridView dg, string name)
    
{
        dg.Visible 
= true;
        Response.Clear();
        Response.Buffer 
= true;
        Response.Charset 
= "GB2312";
        name 
= "attachment;filename=" + name;
        Response.AppendHeader(
"Content-Disposition", name);
        Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType 
= "application/ms-excel";
        dg.EnableViewState 
= false;
        System.IO.StringWriter oStringWriter 
= new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter 
= new System.Web.UI.HtmlTextWriter(oStringWriter);
        dg.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();
    }

    
//输出到word的函数,可直接copy到 cs页面
    private void OutWord(GridView dg, string name)
    
{
        dg.Visible 
= true;
        Response.Clear();
        Response.Buffer 
= true;
        Response.Charset 
= "GB2312";
        name 
= "attachment;filename=" + name;
        Response.AppendHeader(
"Content-Disposition", name);
        Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType 
= "application/ms-word";
        dg.EnableViewState 
= false;
        System.IO.StringWriter oStringWriter 
= new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter 
= new System.Web.UI.HtmlTextWriter(oStringWriter);
        dg.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();
    }

    
//重载VerifyRenderingInServerForm方法,调用页面必须加入否则会提示错误
   public override void VerifyRenderingInServerForm(Control control)
    

    }

    
//调用方法 OutWord(Student, "File name.doc");
    protected void Button1_Click(object sender, EventArgs e)
    
{       
        OutWord(Student, 
"File name.doc");

    }

    
//调用方法 OutExcel(Student, "File name.xls"); 
    protected void Button2_Click(object sender, EventArgs e)
    
{
        OutExcel(Student, 
"File name.xls");        
    }

原网址:http://hi.baidu.com/sharp528108/blog/item/03217012279e3c52f819b884.html
为了避免标头出现乱码
用下边代码对标题进行编码
System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)+".xls";

转载于:https://www.cnblogs.com/sunheyubo/archive/2008/03/27/1124903.html

相关文章:

越来越不想写代码了

越来越不想写代码了今天刚下载了PD12版本,做了一个需求分析以及试用了几个新功能在对C#支持的情况。感觉非常不错,特别是在代码生成方面,比我想的要好多了,而且比起其它语言的代码生成,好像就是对C&#xf…

【代码笔记】Web-CSS-CSS id和Class选择器

一&#xff0c;效果图。 二&#xff0c;代码。 <!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title>CSS id和class选择器</title> <style> #para1 { text-align: center; color: red; } .center { text-align: …

javascript取得鼠标的位置

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns"http://www.w3.org/1999/xhtml"><head><title>JavaScript得到鼠标坐标<…

nginx缓存功能的设置

首先用的缓存是proxy_cache. 在http段里加入下列几句&#xff1a; [plain] view plaincopy proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_tem…

线性表List的基本创建

#include <iostream> using namespace std; #include <malloc.h> #include <stdio.h> #define TURE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define IINFEASIBLE -1 #define OVERFLOW -2 typedef int Status; /*类型名定义用status代替int;Sta…

点点看   只有想不到没有看不到

看了这些好看的图片&#xff0c;不禁也想把自己看到的图片与大家一起分享了&#xff01;[url]http://mm-nn.com.cn/MM[/url]网[url]http://haotupic.cn/[/url]好图片网[url]http://www.picguan.cn/[/url]美女图片馆[url]http://www.nvyoou.cn/[/url]女友网[url]http://www.soft…

二叉树的基本应用知识总结

#include <iostream> using namespace std; #include <malloc.h> #include <stdio.h> #define TURE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define IINFEASIBLE -1 #define OVERFLOW -2#define MAX_TREE_SIZE 100//二叉树的最大结点数 typedef ch…

JS原生选项卡 – 幻灯片效果

1 <!DOCTYPE HTML>2 <html>3 <head>4 <meta charset"utf-8">5 <title>JS实现幻动片选项卡</title>6 </head>7 <style>8 .container{9 text-align:center; 10 width:100%; 11 } 12 13 .ppt{ …

MSSQL 2008里事务的一个问题

今天在试MSSQL2008里的事务&#xff0c;发现如果事务中某条语句的表名错误&#xff0c;就无法用error或try回滚&#xff0c;具体如下&#xff1a;begin tran delete from test where id 5 --正确语句 dealete from testa where id1 4 --表名错误&#xff0c;testa 表…

避免在JSP中写java代码

作者&#xff1a;蜗牛学院CTO李懿老师 ​自从十年前的taglibs&#xff08;如JSTL&#xff09;和EL&#xff08;表达语言&#xff0c;这些事情&#xff09;诞生以来&#xff0c;在JSP中使用scriptlet&#xff08;<% %>这些东西&#xff09;的确是非常不鼓励的。 小脚本的主…

爱情也许是最忧伤的童话

也许爱情是一部忧伤的童话 惟其遥远与真实 惟其不可触摸与欠缺 方可成就起璀璨与神圣 或许&#xff0c;只有在难得最远的时候&#xff0c; 才能把曾经走过的那段日子 看得最真切、最清楚 放弃一个很爱你的人&#xff0c;并不痛苦 放弃一个你很爱的人&#xff0c;那才痛苦 爱…

Docker最全教程——从理论到实战(六)

Docker最全教程——从理论到实战&#xff08;六&#xff09; 原文:Docker最全教程——从理论到实战&#xff08;六&#xff09;托管到腾讯云容器服务 托管到腾讯云容器服务&#xff0c;我们的公众号“magiccodes”已经发布了相关的录屏教程&#xff0c;大家可以结合本篇教程一起…

c#中分割字符串的几种方法

第一种方法&#xff1a;打开vs.net新建一个控制台项目。然后在Main()方法下输入下面的程序。 string s"abcdeabcdeabcde"; string[] sArrays.Split(c); foreach(string i in sArray) Console.WriteLine(i.ToString()); 输出下面的结果:ab deab deab de 我们看到了结果…

对相机所看的视角截屏保存为图片

对相机所看的视角截屏保存为图片&#xff1a; 1 using UnityEngine;2 using System.Collections;3 using UnityEngine.UI;4 /// <summary>5 /// 对相机截图6 /// </summary>7 public class Jietu : MonoBehaviour {8 9 public Camera camera; 10 Texture2D…

fsum函数测试以及分析

#include <stdio.h> #include <stdlib.h> #include <string.h>#define BUFSIZE 256//这是一句预定义&#xff0c;定义BUFSIZE的值是100&#xff0c;是缓冲空间的大小,作为数组的。int main(int argc, char *argv[]) //第一个int argc&#xff0c;是记录你输入…

oracle学习总结二(转义字符)

1、oracle 特殊字符 转义关键词&#xff1a; oracle 转义 环境&#xff1a;oracle 9i plsql在plsql里边执行:update userinfo set pageurlmyjsp?page1&pagesize10 where idtest这条sql语句往数据库的pageurl字段放进去了一…

C语言——冒泡法排序应用

#include <stdio.h> #include <stdlib.h> int main()/*有int main 就会有return 0;如果是void main ,就不用加上return 0了 */ {int i,j,temp;/*待会要用到&#xff0c;所以先定义,因为输入的都是整数&#xff0c;所以用int整数型*/int a[10];/*定义一个一维整型数…

2017 Multi-University Training Contest 3 hdu 6063

HDU 6063 思路&#xff1a; AC代码&#xff1a; #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "m…

[学习]GridView 学习集合 -- GridView中使用超链接的技巧

GridView中使用超链接的技巧 数据绑定方式有两种&#xff0c;如下示例&#xff1a; Eval方式 &#xff1c;%# Eval("id") %&#xff1e; Bind方式 &#xff1c;%# Bind("id","~/info.aspx?id{0}") %&#xff1e; 推荐使用第一种方式&#xff0c…

C# http 性能优化500毫秒到 60 毫秒

来源&#xff1a;https://www.cnblogs.com/hnsongbiao/p/9815808.html 偶然发现 C# 的 HttpRequest 要比 Chrome 请求同一Url 慢好多。C# HttpRequest 要500毫秒 而Chrome 只需要 39ms。 后来 整理 各种方法做了优化 HttpWebRequest request WebRequest.Create(address) as H…

一个计算机高手的成长(转)

这些日子我一直在写一个实时操作系统内核&#xff0c;已有小成了&#xff0c;等写完我会全部公开&#xff0c;希望能 够为国内IT的发展尽自己一份微薄的力量。最近看到很多学生朋友和我当年一样没有方向 &#xff0c;所以把我的经历写出来与大家共勉&#xff0c;希望能给刚如行…

正则表达式整理

1.特殊字符 ^匹配输入字符串的开始位置$匹配输入字符串的结尾位置( )标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用。要匹配这些字符&#xff0c;请使用 \( 和 \)。* 匹配前面的子表达式零次或多次。要匹配 * 字符&#xff0c;请使用 \*。匹配前面的子表达式一…

CSS-hover

1. padding:0 10px; 表示上下边距是0&#xff0c;左右边距是10. 2. padding:0 10px 0 10px&#xff1b; 上-右-下-左。&#xff08;顺时针方向&#xff09; 3. .pg-header .menu:hover&#xff1b; 表示只要鼠标移动到当前标签上时&#xff0c;就会应用这个下面所定义的样式。 …

Error原生类型

•表示错误对象 –EvalError, URIError, RangeError, etc. •捕获方式&#xff1a; –try { …throw new Error(…) } catch(e) { … } –理论上可以throw出任意对象 •Error对象IE和FireFox公有属性 –message&#xff1a;错误信息Error浏览器特定属性 •IE&#xff1a; –des…

求矩阵两条对角线元素之和

#include <stdio.h> #include <stdlib.h>int main() {int a[3][3];//定义一个二维数组&#xff0c;三行三列&#xff0c;a[0][0],a[0][1],a[0][2],a[1][0],a[1][1],a[1][2],a[3][0],a[3][1],a[3][2]int i,j,sum0;printf("请输入9个数据给数组赋值:\n");f…

关于Java中各种修饰符与访问修饰符的说明

关于Java中各种修饰符与访问修饰符的说明 类&#xff1a; 访问修饰符 修饰符 class 类名称extends 父类名称 implement 接口名称 &#xff08;访问修饰符与修饰符的位置可以互换&#xff09;访问修饰符 名称 说明 备注 public可以被所有类访问&#xff08;使用&#xff0…

buffer IO和direct IO

最近在看很多框架&#xff0c;redis&#xff0c;kafka等底层都涉及到文件IO操作的效率问题&#xff0c;所以查了些资料&#xff0c;看到这篇文章讲的比较明白些&#xff0c;贴出来留存。 链接地址&#xff1a; https://www.ibm.com/developerworks/cn/linux/l-cn-directio/转载…

python 列表使用

下面实现的类似于java中的数组&#xff1a; names[-2]表示实现倒数的第2个参数 names[-3,-1]表示实现-3到-1的值不包含-1 增删改查 下面代码实现列表的增删改查功能&#xff1a; 复制copy 深copy:这个可以copy全部的参数列表&#xff08;一般不用&#xff09;。 For循环打印列表…

c语言——求单词个数

#include <stdio.h> #include <stdlib.h>int main() {char str[20];//定义一个字符型一维数组&#xff0c;长度为20int i,num;printf("请输入一行英文:\n");//提示语句gets(str);//输入一个字符串给字符数组strnum str[0]! ? 1:0;//? : 是条件运算符…

C# 各种进制之间相互转换 [ZT]

MessageBox.Show(Convert.ToString(12345, 2)); //10进制转换为2进制MessageBox.Show(Convert.ToString(12345, 8)); //10进制转换为8进制MessageBox.Show(Convert.ToString(12345, 16)); //10进制转换为16进制MessageBox.Show(Convert.ToInt32("1100", 2).ToStri…