excel类, 直接向excel 文件中写
解决方案 »
- 问个算术题。。
- 关于文件的备份
- DATAGRIDVIEW中CHECKBOX选后行的值相加问题
- C#操作Excel表并与其同步
- 急:c# 中teechart 控件如何与数据库绑定的问题
- C#能否写COM,如何写?
- SharpDevelop 2.1版 下载下来后编译StartUp项目出错
- 关于EventHandler的程序,在控制台和winform下移植的问题
- TCP连接正常,发送数据后,无法返回数据
- 使用XmlTextReader读取XML时,如果存在重复节点的时候怎么处理
- 谁知道用C#开发windows应用程序的视频教程
- 请问如何把数据库中的图片读出生成缩略图?(sql server+c#.net)
页面增加一个按钮,单击事件添加如下方法:
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生成绩报表.xls");
} private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
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);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
}
还有由于是文件操作所以要引入名称空间IO和Text后台代码:
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.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
public partial class Default7 : System.Web.UI.Page
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind(); }
}
public void bind()
{
string sqlstr = "select top 5 * from 飞狐工作室";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "飞狐工作室");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "身份证号码" };
GridView1.DataBind();
sqlcon.Close();
} protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生成绩报表.xls");
} private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
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);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}前台:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" >
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
<asp:BoundField DataField="姓名" HeaderText="姓名" />
<asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
<asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
<asp:BoundField DataField="起薪" HeaderText="起薪" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
具体后台代码怎么写?谢谢
win下的代码可以参考如下网址:
http://www.it130.net/Csharp/WinForm-dataGridView-Excel-2176.htm
{
Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();//创建Excel表
myExcel.Visible = true;
Microsoft.Office.Interop.Excel.Workbooks myWorkbooks = myExcel.Workbooks;//创建Excel工作表
Microsoft.Office.Interop.Excel.Workbook myWorkbook = myWorkbooks.Add(System.Reflection.Missing.Value); //创建Excel工作表
Microsoft.Office.Interop.Excel.Worksheet myWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkbook.Worksheets[1];//创建Excel工作表Sheet1页
Microsoft.Office.Interop.Excel.Range myrange = myWorksheet.get_Range("A1", "D1");//选择页的范围,从“A1”开始,我这是四列所以到“D1” object[] myhead = { "日期", "组名", "付款人", "金额" };//设置表头
myrange.Value2 = myhead;//表头的值传入Excel表 if (dgvRecord.Rows.Count > 0)
{
myrange = myWorksheet.get_Range("A2", System.Reflection.Missing.Value);
int row = 0;
row = dgvRecord.Rows.Count;//设置表的行数
int col = 4;//设置表的列数
object[,] mydata = new object[row, col];
for (int i = 0; i < row; i++)//循环读取DataGridView上的数据
{
for (int j = 0; j < col; j++)
{
mydata[i, j] = dgvRecord[j, i].Value.ToString();//读取DataGridView上的值保存到mydata数组中,这里要注意哦,DataGridView是列在前,而Excel刚好相反
}
}
myrange = myrange.get_Resize(row, col);
myrange.Value2 = mydata;//把madata数组的值存到Excel表导出
myrange.EntireColumn.AutoFit();
}
myExcel = null;//清空表单
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}