asp.net 导出Excel方法汇总
第一种:需要引用com :microsoft.excel.11.0.
//生成Excel文件的代码
protected void ExportExcel()
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); // 创建工作簿
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1]; // 创建工作页
DataSet ds = GetData();
int iMaxRow = ds.Tables["rsData"].Rows.Count;
int iMaxCol = ds.Tables["rsData"].Columns.Count;
// 设置格式
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iMaxCol]).Font.Name = "黑体";
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iMaxCol]).Font.Bold = true;
ws.get_Range(ws.Cells[1, 1], ws.Cells[iMaxRow + 1, iMaxCol]).Borders.LineStyle = 1;
// 设置标题
excel.Cells[1, 1] = "班级";
excel.Cells[1, 2] = "学号";
excel.Cells[1, 3] = "姓名";
excel.Cells[1, 4] = "性别";
// 填充数据
for (int iRow = 0; iRow < iMaxRow; iRow++)
{
for (int iCol = 0; iCol < iMaxCol; iCol++)
{
excel.Cells[iRow + 2, iCol + 1] = ds.Tables["rsData"].Rows[iRow][iCol].ToString();
}
}
// 保存Excel
excel.Save("学生基础信息");
// 打开Excel
excel.Visible = true;
}
第二种:利用 System.IO
public void CreateExcel(DataSet ds, string typeid, string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.ContentType = "application/ms-excel";
resp.AddHeader("Content-Dispositi
相关文档:
问题描述:
Asp.Net中datalist等web控件里面,放多个单选按钮的时候可以同时多选,可以采取以下放法。
问题解决:
最理想的解决之道,用javascript:
<script language="javascript" type="text/javascript">
function clickit() {
var dom=document.all;
& ......
数字/字母混合很简单的,看着比较舒服,前台生成的aspx文件我就不贴出来了,默认的,我也未作修改。下面只贴出后台的cs代码。仅供参考。
public partial class CheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CreateCheckCodeImage( ......
JSON Serialization and Deserialization in ASP.Net
I was looking around for a simple example which would just do an object serialization to a JSON format, and then deserializing back to the original object. I found few examples on MSDN, but did seem to be too long ......
ASP.NET页面跳转有什么方法呢?,现在给大家介绍三种方法,他们的区别是什么呢?让我们开始吧:
ASP.NET页面跳转1、response.redirect 这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但他可以跳转到任何页面,没有站点页面限制(即可以由雅虎跳到新浪),同时不能跳过登录保护。但速度慢是其最大缺陷! ......
3 mistakes to avoid when using jQuery with ASP.NET AJAX
AJAX, ASP.NET, JavaScript, jQuery By Dave Ward on June 5th, 2008
Over the past few weeks, I think I have definitely embodied Jeff Atwood’s claim that we’re all amateurs, learning together. Despite my best efforts to thoroughly tes ......