您的位置:首页 > 编程语言 > ASP

在aspnet中将girdview中间连续几项的信息导出excel

2013-02-04 16:49 381 查看
图片效果

gridview中的样子



导出的excel中的样子



没有导出记录时



方法:

导出excel按钮下面的代码

if (gvOlders.Rows.Count == 0)

{

ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('没有记录可以导出!')</script>");

}

else

{

OutDataToExcel(gvOlders, mon);

}

OutDataToExcel方法

public void OutDataToExcel(System.Web.UI.WebControls.GridView gridview, IList<BasicInfo> list)

{

gvOlders.Columns[0].Visible = false;

gvOlders.Columns[8].Visible = false;

HttpContext hc = HttpContext.Current;

ArrayList al = new ArrayList();

int count = gridview.Columns.Count - 1;

for (int i = 0; i < count; i++)

{

string t = gvOlders.Columns[i].HeaderText;

al.Add(t);

}

AppLibrary.WriteExcel.XlsDocument doc = new AppLibrary.WriteExcel.XlsDocument();

//给文件取名字

doc.FileName = DateTime.Now.ToString("yyyyMMddHHMMss") + ".xls";

string SheetName = string.Empty;

SheetName = "数据查询";

AppLibrary.WriteExcel.Worksheet sheet = doc.Workbook.Worksheets.Add(SheetName);

AppLibrary.WriteExcel.Cells cells = sheet.Cells;

#region 第一个样式

AppLibrary.WriteExcel.XF XFstyle = doc.NewXF();//添加样式的

XFstyle.HorizontalAlignment = AppLibrary.WriteExcel.HorizontalAlignments.Centered;

XFstyle.Font.FontName = "宋体";//字体

XFstyle.UseMisc = true;

XFstyle.TextDirection = AppLibrary.WriteExcel.TextDirections.LeftToRight;//文本位置

XFstyle.Font.Bold = false;//加粗

#region 边框线的样式

XFstyle.BottomLineStyle = 1;

XFstyle.LeftLineStyle = 1;

XFstyle.TopLineStyle = 1;

XFstyle.RightLineStyle = 1;

#endregion

XFstyle.UseBorder = true;

XFstyle.PatternBackgroundColor = AppLibrary.WriteExcel.Colors.Blue;

XFstyle.PatternColor = AppLibrary.WriteExcel.Colors.White;

XFstyle.Pattern = 1;

#region 宽度

AppLibrary.WriteExcel.ColumnInfo colInfo = new AppLibrary.WriteExcel.ColumnInfo(doc,

sheet);

colInfo.ColumnIndexStart = 0;

colInfo.ColumnIndexEnd = 4;

colInfo.Width = 10 * 256;

sheet.AddColumnInfo(colInfo);

AppLibrary.WriteExcel.ColumnInfo colInfo1 = new AppLibrary.WriteExcel.ColumnInfo

(doc, sheet);

colInfo1.ColumnIndexStart = 5;

colInfo1.ColumnIndexEnd = 7;

colInfo1.Width = 15 * 256;

sheet.AddColumnInfo(colInfo1);

AppLibrary.WriteExcel.ColumnInfo colInfo2 = new AppLibrary.WriteExcel.ColumnInfo

(doc, sheet);

colInfo2.ColumnIndexStart = 8;

colInfo2.ColumnIndexEnd = 9;

colInfo2.Width = 15 * 256;

sheet.AddColumnInfo(colInfo2);

AppLibrary.WriteExcel.ColumnInfo colInfo3 = new AppLibrary.WriteExcel.ColumnInfo

(doc, sheet);

colInfo3.ColumnIndexStart = 12;

colInfo3.ColumnIndexEnd = 12;

colInfo3.Width = 15 * 256;

sheet.AddColumnInfo(colInfo3);

AppLibrary.WriteExcel.ColumnInfo colInfo4 = new AppLibrary.WriteExcel.ColumnInfo

(doc, sheet);

colInfo4.ColumnIndexStart = 13;

colInfo4.ColumnIndexEnd = 13;

colInfo4.Width = 50 * 256;

sheet.AddColumnInfo(colInfo4);

AppLibrary.WriteExcel.ColumnInfo colInfo5 = new AppLibrary.WriteExcel.ColumnInfo

(doc, sheet);

colInfo5.ColumnIndexStart = 14;

colInfo5.ColumnIndexEnd = 14;

colInfo5.Width = 15 * 256;

sheet.AddColumnInfo(colInfo5);

AppLibrary.WriteExcel.ColumnInfo colInfo6 = new AppLibrary.WriteExcel.ColumnInfo

(doc, sheet);

colInfo6.ColumnIndexStart = 15;

colInfo6.ColumnIndexEnd = 15;

colInfo6.Width = 50 * 256;

sheet.AddColumnInfo(colInfo6);

#endregion

#endregion

#region 添加表头

int idx = 1;

for (int i = 1; i < al.Count; i++)

{

cells.Add(1, idx, al[i], XFstyle);

idx++;

}

#endregion

int f = 2;//从第二行开始填充数据

foreach (BasicInfo m in list)

{

cells.Add(f, 1, m.OlderName, XFstyle);

cells.Add(f, 2, m.Birthday.ToShortDateString(), XFstyle);

cells.Add(f, 3, m.Age, XFstyle);

cells.Add(f, 4, m.Sex, XFstyle);

cells.Add(f, 5, m.Pid, XFstyle);

cells.Add(f, 6, m.SelfDepend, XFstyle);

cells.Add(f, 7, m.State, XFstyle);

f++;

}

doc.Send();

hc.Response.Flush();

hc.Response.End();

}

mon页面级变量

public List<BasicInfo> mon

{

get { return ViewState["mon"] as List<BasicInfo>; }

set { ViewState["mon"] = value; }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: