首页 > 代码库 > Excel导出下拉框引用数据
Excel导出下拉框引用数据
namespace WebInfo
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet realSheet = (HSSFSheet)workbook.CreateSheet("Sheet xls");
HSSFSheet hidden = (HSSFSheet)workbook.CreateSheet("hidden");
string[] pgName = GetDataStr();
for (int i = 0, length = pgName.Length; i < length; i++)
{
String name = pgName[i];
HSSFRow row = (HSSFRow)hidden.CreateRow(i);
HSSFCell cell = (HSSFCell)row.CreateCell(0); cell.SetCellValue(name); } IName namedCell = workbook.CreateName(); namedCell.NameName = "hidden"; namedCell.RefersToFormula = "hidden!$A$1:$A$" + pgName.Length; DVConstraint constraint = DVConstraint.CreateFormulaListConstraint("hidden"); CellRangeAddressList addressList = new CellRangeAddressList(0, 100, 0, 0); HSSFDataValidation validation = new HSSFDataValidation(addressList, constraint); workbook.SetSheetHidden(1, true); realSheet.AddValidationData(validation);
realSheet.SetColumnWidth(0, 8000);
//填充数据 //。。。。。。。。。。。。。
string savePath = this.MapPath("~/Temp/Default.xls"); using (FileStream file = new FileStream(savePath, FileMode.Create)) { workbook.Write(file); } string path = this.MapPath("~/Temp/Default.xls");
ExcelOutE(this, path); } private string[] GetDataStr() { List<string> DataList = new List<string>(); DataList.Add("ThinkCentre M及启天产品"); DataList.Add("笔记本昭阳"); DataList.Add("LCS 新技术孵化器"); DataList.Add("LCS ThinkPad CTO"); DataList.Add("ThinkPad"); DataList.Add("Think KAB"); DataList.Add("LCS ThinkStation CTO"); DataList.Add("ThinkStation"); DataList.Add("Options"); DataList.Add("服务器存储"); DataList.Add("高性能服务器"); DataList.Add("企业级存储-NAS"); DataList.Add("企业级存储-SAN"); DataList.Add("乐Pad"); DataList.Add("行业平板电脑"); DataList.Add("LCS 联想打印机"); DataList.Add("LCS 激光打印机耗材"); DataList.Add("大客户服务"); DataList.Add("LCS 移动存储"); DataList.Add("消费台式电脑"); DataList.Add("LCS 软件"); DataList.Add("LCS 信息服务"); DataList.Add("LCS 存储产品"); DataList.Add("LCS 外设喷墨打印机及耗材"); DataList.Add("LCS IT 1for1教育软件"); DataList.Add("信息安全产品"); DataList.Add("LCS IT 1FOR1"); DataList.Add("LCS 网络产品"); DataList.Add("PC Option"); DataList.Add("产品服务部"); DataList.Add("LCS 服务产品"); DataList.Add("LCS 高端服务器"); DataList.Add("IdeaCentre Option"); DataList.Add("数码维修备件"); DataList.Add("数码影音"); DataList.Add("LCS 存储卡"); DataList.Add("LCS 消费笔记本(旧)"); DataList.Add("商用台式电脑扬天产品"); DataList.Add("LCS BBPC"); DataList.Add("LCS 外设投影机"); DataList.Add("IdeaPad Option"); DataList.Add("LCS无线应用"); DataList.Add("笔记本电脑天逸旭日产品"); DataList.Add("LCS CDB配售业务"); DataList.Add("LCS 随身电脑"); DataList.Add("007产品"); DataList.Add("乐Phone"); DataList.Add("LCS QDI 中国区");
return DataList.ToArray(); }
private void ExcelOutE(Page Page, string fileName) { HttpResponse Response = Page.Response; HttpServerUtility Server = Page.Server;
string fn = Server.UrlDecode(fileName); FileStream fileStream = new FileStream(fn, FileMode.Open); long fileSize = fileStream.Length; fileStream.Close(); Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=Sheet1.xls"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.ContentType = "application/ms-excel"; Response.AddHeader("Content-Length", fileSize.ToString()); Page.EnableViewState = false; Response.WriteFile(fn); Response.Flush(); Response.End(); } }