首页 新闻 会员 周边

asp.net mvc2 怎么 导出excel啊??

0
悬赏园豆:5 [待解决问题]

asp.net mvc2 怎么 导出excel啊??后台系统 效果图:

 

阳光总在风雨...的主页 阳光总在风雨... | 初学一级 | 园豆:195
提问于:2014-04-25 19:11
< >
分享
所有回答(2)
0

跟mvc无关,可使用npoi、closexml开源组件实现导出excel功能,在园内搜,有好多园友的博文的;如http://www.cnblogs.com/tuqun/p/3684733.html 

秋壶冰月 | 园豆:5903 (大侠五级) | 2014-04-25 22:46

是这样的,点击“导出excel”程序生成.xls文件然后 自动下载给操作者,呵呵

支持(0) 反对(0) 阳光总在风雨... | 园豆:195 (初学一级) | 2014-04-30 21:19
0

//导出EXCEL方法
        public MemoryStream ExprotToExcel()
        {
            ReportData report = bo.GetReportNum();
            List<ReportData> reports = bo.GetReport();
            //创建WorkBook对象  
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建表、行、单元格对象:上一级对象.CreateSheet,CreateRow,CreateCell方法来创建
            HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("统计报表");
            for (int i = 0; i <= reports.Count; i++)
            {
                if (i == 0)
                {
                    sheet.CreateRow(0).CreateCell(0).SetCellValue("序号");
                    sheet.GetRow(0).CreateCell(1).SetCellValue("单位名称");
                    sheet.GetRow(0).CreateCell(2).SetCellValue("在职职工数");
                    sheet.GetRow(0).CreateCell(3).SetCellValue("建议数");
                    sheet.GetRow(0).CreateCell(4).SetCellValue("提案数");
                    sheet.GetRow(0).CreateCell(5).SetCellValue("提案人数");
                    sheet.GetRow(0).CreateCell(6).SetCellValue("参与率");
                    sheet.GetRow(0).CreateCell(7).SetCellValue("采纳数");
                    sheet.GetRow(0).CreateCell(8).SetCellValue("采纳率");
                    sheet.GetRow(0).CreateCell(9).SetCellValue("实施数");
                    sheet.GetRow(0).CreateCell(10).SetCellValue("实施率");
                    sheet.GetRow(0).CreateCell(11).SetCellValue("自主实施");
                }
                else
                {
                    sheet.CreateRow(i).CreateCell(0).SetCellValue(i);
                    sheet.GetRow(i).CreateCell(1).SetCellValue(reports[i - 1].company);
                    sheet.GetRow(i).CreateCell(2).SetCellValue(reports[i - 1].zzPerson);
                    sheet.GetRow(i).CreateCell(3).SetCellValue(reports[i - 1].proposalCount);
                    sheet.GetRow(i).CreateCell(4).SetCellValue(reports[i - 1].tiAnCount);
                    sheet.GetRow(i).CreateCell(5).SetCellValue(reports[i - 1].tiAnPersonCount);
                    sheet.GetRow(i).CreateCell(6).SetCellValue(reports[i - 1].AdoptionRate);
                    sheet.GetRow(i).CreateCell(7).SetCellValue(reports[i - 1].tiAnCnCount);
                    sheet.GetRow(i).CreateCell(8).SetCellValue(reports[i - 1].ParticipationRate);
                    sheet.GetRow(i).CreateCell(9).SetCellValue(reports[i - 1].tiASsCount);
                    sheet.GetRow(i).CreateCell(10).SetCellValue(reports[i - 1].ImplementRate);
                    sheet.GetRow(i).CreateCell(11).SetCellValue(reports[i - 1].tiAZzSs);
                }
                if (i == 39)
                {
                    sheet.CreateRow(i + 1).CreateCell(0).SetCellValue("公司汇总");
                    sheet.GetRow(i + 1).CreateCell(1).SetCellValue("");
                    sheet.GetRow(i + 1).CreateCell(2).SetCellValue(report.zzPerson);
                    sheet.GetRow(i + 1).CreateCell(3).SetCellValue(report.proposalCount);
                    sheet.GetRow(i + 1).CreateCell(4).SetCellValue(report.tiAnCount);
                    sheet.GetRow(i + 1).CreateCell(5).SetCellValue(report.tiAnPersonCount);
                    sheet.GetRow(i + 1).CreateCell(6).SetCellValue(report.AdoptionRate);
                    sheet.GetRow(i + 1).CreateCell(7).SetCellValue(report.tiAnCnCount);
                    sheet.GetRow(i + 1).CreateCell(8).SetCellValue(report.ParticipationRate);
                    sheet.GetRow(i + 1).CreateCell(9).SetCellValue(report.tiASsCount);
                    sheet.GetRow(i + 1).CreateCell(10).SetCellValue(report.ImplementRate);
                    sheet.GetRow(i + 1).CreateCell(11).SetCellValue(report.tiAZzSs);
                    sheet.AddMergedRegion(new CellRangeAddress(i + 1, i + 1, 0, 1));
                }
            }
            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;

                return ms;
            }

        }

土匪吃黄瓜 | 园豆:2 (初学一级) | 2014-05-07 11:15

 我自己已经解决了,不过谢谢,我使用的方法是:

public ActionResult GetFileFromDisk(FormCollection forms, FeiPiao.Model.FP_ZYB_OrderProcessVM opModel)
{
int total = 0;
DataSet list = FeiPiao.BLL.ZYB_OrderProcessBLL.GetInstance().GetZYB_OrderProcessList(opModel, ref total);
#region"设置表头"
string[] headNames = null;
string[] colsNames = null;
headNames = new string[] { "联系人", "手机号", "智游宝订单号", "订单号", "景区名称", "订单来源", "结算编号", "产品名称", "票数量", "票价(卖)", "总价", "状态", "使用状态", "验票时间", "创建时间" };
colsNames = new string[] { "LinkName", "LinkMobile", "ZYBOrderCode", "OrderCode", "ScenicName", "ProductPlatName", "FinancialCode", "ProductTitle", "Quantity", "Price", "TotalPrice", "TS_Name", "S_Name", "RecountTime", "CreateDate" };
#endregion
FeiPiao.Utility.DataToExcel dte = new FeiPiao.Utility.DataToExcel();
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";
string path = dte.DataExcel(list, "智游宝订单列表-" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), fileName, headNames, colsNames);
if (Request.Browser.Browser == "IE")
{
fileName = Server.UrlPathEncode(fileName);
}
Response.ContentType = "application/ms-excel";
//string strcontentDisposition = string.Format("{0};filename=\"{1}\"", "attachment", fileName);
//Response.AppendHeader("Content-Disposition", strcontentDisposition);
return File(path, "application/ms-excel", fileName);
}

 

 

public string DataExcel(DataSet ds, string strTitle, string filename, string[] headNames, string[] colsNames)
{
if (!(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0))
{
//OperationLayer.Utility.MessageBox.CustomJs(this.Page, "alert('没有数据不能导出Excel!');window.history.back(-1);");
return string.Empty;
}
string ExcelFolder = "Excel";
string FilePath = AppDomain.CurrentDomain.BaseDirectory + ExcelFolder + "\\";
FilePath = FilePath + filename;
COM.Excel.cExcelFile excel = new COM.Excel.cExcelFile();
excel.CreateFile(FilePath);
excel.PrintGridLines = false;

COM.Excel.cExcelFile.MarginTypes mt1 = COM.Excel.cExcelFile.MarginTypes.xlsTopMargin;
COM.Excel.cExcelFile.MarginTypes mt2 = COM.Excel.cExcelFile.MarginTypes.xlsLeftMargin;
COM.Excel.cExcelFile.MarginTypes mt3 = COM.Excel.cExcelFile.MarginTypes.xlsRightMargin;
COM.Excel.cExcelFile.MarginTypes mt4 = COM.Excel.cExcelFile.MarginTypes.xlsBottomMargin;

double height = 2;
excel.SetMargin(ref mt1, ref height);
excel.SetMargin(ref mt2, ref height);
excel.SetMargin(ref mt3, ref height);
excel.SetMargin(ref mt4, ref height);

COM.Excel.cExcelFile.FontFormatting ff = COM.Excel.cExcelFile.FontFormatting.xlsNoFormat;
string font = "宋体";
short fontsize = 9;
excel.SetFont(ref font, ref fontsize, ref ff);

byte b1 = 1,
b2 = 12;
short s3 = 12;
excel.SetColumnWidth(ref b1, ref b2, ref s3);

string header = "页眉";
string footer = "页脚";
excel.SetHeader(ref header);
excel.SetFooter(ref footer);


COM.Excel.cExcelFile.ValueTypes vt = COM.Excel.cExcelFile.ValueTypes.xlsText;
COM.Excel.cExcelFile.CellFont cf = COM.Excel.cExcelFile.CellFont.xlsFont0;
COM.Excel.cExcelFile.CellAlignment ca = COM.Excel.cExcelFile.CellAlignment.xlsCentreAlign;
COM.Excel.cExcelFile.CellHiddenLocked chl = COM.Excel.cExcelFile.CellHiddenLocked.xlsNormal;

// 报表标题
int cellformat = 1;

//int rowindex = 1, colindex = 3;
//object title = (object)strTitle;
//excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowindex, ref colindex, ref title, ref cellformat);

int rowIndex = 1;//起始行
int colIndex = 0;

 

try
{
#region 取得列标题
//取得列标题
//foreach (DataColumn colhead in dt.Columns)
//{
// colIndex++;
// string name = colhead.ColumnName.Trim();
// object namestr = (object)name;
// IDictionaryEnumerator Enum = nameList.GetEnumerator();
// while (Enum.MoveNext())
// {
// if (Enum.Key.ToString().Trim() == name)
// {
// namestr = Enum.Value;
// }
// }
// excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref namestr, ref cellformat);
//}
#endregion

foreach (string headName in headNames)
{
colIndex++;
object namestr = headName;
excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref namestr, ref cellformat);
}

#region 取得表格中的数据
//取得表格中的数据
//foreach (DataRow row in dt.Rows)
//{
// rowIndex++;
// colIndex = 0;
// foreach (DataColumn col in dt.Columns)
// {
// colIndex++;
// //if (col.DataType == System.Type.GetType("System.DateTime"))
// //{
// // object str = (object)(Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd"); ;
// // excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str, ref cellformat);
// //}
// //else
// //{
// object str = (object)row[col.ColumnName].ToString();
// excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str, ref cellformat);
// //}
// }
//}
#endregion

foreach (DataRow row in ds.Tables[0].Rows)
{
rowIndex++;
colIndex = 0;
foreach (string colsName in colsNames)
{
colIndex++;
object str = (object)row[colsName].ToString();
excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str, ref cellformat);
}
}
}
catch (Exception ex)
{
FilePath = string.Empty;
}
finally
{
excel.CloseFile();
}
return FilePath;

}

支持(0) 反对(0) 阳光总在风雨... | 园豆:195 (初学一级) | 2014-05-07 11:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册