asp.net 导入和导出Excel
2009-09-15 12:10
405 查看
导出:
导入模板
string filePath = Server.MapPath("~/TemplateFile/按天计薪模板.xls");
string newpath = Server.MapPath("~/TemplateFile/" + Guid.NewGuid() + ".xls");
File.Copy(filePath, newpath);
File.SetAttributes(newpath, FileAttributes.Normal);
Stream s = File.Open(newpath, FileMode.Open);
//----------创建Excel
HSSFWorkbook wb = new NPOI.HSSF.UserModel.HSSFWorkbook(s);
s.Close();
File.Delete(newpath);
HSSFSheet sheet = wb.GetSheetAt(0);
string strAdd = string.Empty;
string strnull = string.Empty;
int rowCount = sheet.LastRowNum - 1;
StringBuilder sbError = new StringBuilder();
HSSFCell cell;
string where = " AND Dealed=1 AND AuditingState=’finished’ AND RewardType=’day’ " + strwhere;
DataTable dt = ProjecUsertDAL.GetPartTimeReward_WFLog(where);
//创建行
HSSFRow row2 = sheet.GetRow(3);
if (row2 == null)
row2 = new HSSFRow(wb, sheet, 3);
if (row2 != null)
{
string pid = Request["pid"];
if (pid != null)
{
//获取单元格
HSSFCell cell2 = row2.GetCell(1);
cell2.SetCellValue(ProjectNO);
HSSFCell cell6 = row2.GetCell(6);
cell6.SetCellValue(ProjectName);
HSSFCell cell9 = row2.GetCell(9);
cell9.SetCellValue(date);
HSSFCell cell12 = row2.GetCell(12);
cell12.SetCellValue(Request["city"]);
#region 输出Excel下载
wb.Write(Response.OutputStream);
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName("按天计薪模板.xls"), System.Text.Encoding.UTF8));
Response.ContentType = "application/octet-stream";
Response.Flush();
Response.End();
#endregion
导入:
// 获取导入的路径
string filePath = Path.Combine(Request.PhysicalApplicationPath, string.Format("Temp//{0}", FileUpload1.FileName));
Stream s = File.Create(filePath);
s.Write(FileUpload1.FileBytes, 0, FileUpload1.FileBytes.Length);
// 创建Excel
HSSFWorkbook wb = new NPOI.HSSF.UserModel.HSSFWorkbook(s);
HSSFSheet sheet = wb.GetSheetAt(0);
string strAdd =string.Empty;
string strnull = string.Empty;
int rowCount = sheet.LastRowNum -1;
StringBuilder sbError = new StringBuilder();
for (int i = 2; i < rowCount; i++)
{
HSSFRow row = sheet.GetRow(i);
strnull = CheckValid(row);
if (string.IsNullOrEmpty(strnull) == false)
{
sbError.Append( string.Format("<tr><td style=’cursor:hand;’ onclick=/"document.all.r{0}.style.display =document.all.r{0}.style.display == ’’?’none’:’’;/">第{0}行导入失败!<div id=’r{0}’ style=’display:none’>原因为:{1}</div></td></tr>"
,i+1 , strnull ));
continue;
}
else
{
strAdd = AddData(row);
if (string.IsNullOrEmpty(strAdd) == false)
{
sbError.Append(string.Format("<tr><td style=’cursor:hand;’ onclick=/"document.all.r{0}.style.display =document.all.r{0}.style.display == ’’?’none’:’’;/">第{0}行导入失败!<div id=’r{0}’ style=’display:none’>原因为:{1}</div></td></tr>"
, i + 1, strAdd));
}
}
strAdd = string.Empty;
strnull = string.Empty;
}
s.Close();
File.Delete(filePath);
if (sbError.Length < 1)
{
labMsg.Text = "导入成功!";
}
else
{
labMsg.Text = string.Format("<table class=’errorMsg’>{0}</table>",sbError.ToString());
}
控件下载地址:http://www.openso.net/downtool?id=264
导入模板
string filePath = Server.MapPath("~/TemplateFile/按天计薪模板.xls");
string newpath = Server.MapPath("~/TemplateFile/" + Guid.NewGuid() + ".xls");
File.Copy(filePath, newpath);
File.SetAttributes(newpath, FileAttributes.Normal);
Stream s = File.Open(newpath, FileMode.Open);
//----------创建Excel
HSSFWorkbook wb = new NPOI.HSSF.UserModel.HSSFWorkbook(s);
s.Close();
File.Delete(newpath);
HSSFSheet sheet = wb.GetSheetAt(0);
string strAdd = string.Empty;
string strnull = string.Empty;
int rowCount = sheet.LastRowNum - 1;
StringBuilder sbError = new StringBuilder();
HSSFCell cell;
string where = " AND Dealed=1 AND AuditingState=’finished’ AND RewardType=’day’ " + strwhere;
DataTable dt = ProjecUsertDAL.GetPartTimeReward_WFLog(where);
//创建行
HSSFRow row2 = sheet.GetRow(3);
if (row2 == null)
row2 = new HSSFRow(wb, sheet, 3);
if (row2 != null)
{
string pid = Request["pid"];
if (pid != null)
{
//获取单元格
HSSFCell cell2 = row2.GetCell(1);
cell2.SetCellValue(ProjectNO);
HSSFCell cell6 = row2.GetCell(6);
cell6.SetCellValue(ProjectName);
HSSFCell cell9 = row2.GetCell(9);
cell9.SetCellValue(date);
HSSFCell cell12 = row2.GetCell(12);
cell12.SetCellValue(Request["city"]);
#region 输出Excel下载
wb.Write(Response.OutputStream);
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName("按天计薪模板.xls"), System.Text.Encoding.UTF8));
Response.ContentType = "application/octet-stream";
Response.Flush();
Response.End();
#endregion
导入:
// 获取导入的路径
string filePath = Path.Combine(Request.PhysicalApplicationPath, string.Format("Temp//{0}", FileUpload1.FileName));
Stream s = File.Create(filePath);
s.Write(FileUpload1.FileBytes, 0, FileUpload1.FileBytes.Length);
// 创建Excel
HSSFWorkbook wb = new NPOI.HSSF.UserModel.HSSFWorkbook(s);
HSSFSheet sheet = wb.GetSheetAt(0);
string strAdd =string.Empty;
string strnull = string.Empty;
int rowCount = sheet.LastRowNum -1;
StringBuilder sbError = new StringBuilder();
for (int i = 2; i < rowCount; i++)
{
HSSFRow row = sheet.GetRow(i);
strnull = CheckValid(row);
if (string.IsNullOrEmpty(strnull) == false)
{
sbError.Append( string.Format("<tr><td style=’cursor:hand;’ onclick=/"document.all.r{0}.style.display =document.all.r{0}.style.display == ’’?’none’:’’;/">第{0}行导入失败!<div id=’r{0}’ style=’display:none’>原因为:{1}</div></td></tr>"
,i+1 , strnull ));
continue;
}
else
{
strAdd = AddData(row);
if (string.IsNullOrEmpty(strAdd) == false)
{
sbError.Append(string.Format("<tr><td style=’cursor:hand;’ onclick=/"document.all.r{0}.style.display =document.all.r{0}.style.display == ’’?’none’:’’;/">第{0}行导入失败!<div id=’r{0}’ style=’display:none’>原因为:{1}</div></td></tr>"
, i + 1, strAdd));
}
}
strAdd = string.Empty;
strnull = string.Empty;
}
s.Close();
File.Delete(filePath);
if (sbError.Length < 1)
{
labMsg.Text = "导入成功!";
}
else
{
labMsg.Text = string.Format("<table class=’errorMsg’>{0}</table>",sbError.ToString());
}
控件下载地址:http://www.openso.net/downtool?id=264
相关文章推荐
- ASP.NET NPOI Excel导入、导出,高级、全面、总结版
- asp.net中Excel导入导出控件 2.0升级版本和1.0版本代码开源
- ASP.Net MVC利用NPOI导入导出Excel
- ASP.NET导出导入Excel
- asp.net Excel导入和导出
- ASP.NET中导入和导出标准Excel文件(转)
- asp.net 客户端导出excel 真正的excel 可以导入 源码
- ExcelHelper(Excel和C#、asp.net导入导出,通用类)(一)
- asp.net导入导出EXCEL
- Asp.net导入导出Excel总结
- ASP.NET对EXCEL的导入导出
- ASP.NET Excel导入导出SQL Server整理(转)
- ASP.NET之Excel下载模板、导入、导出操作
- c#.net 使用NPOI导入导出标准Excel (asp.net winform csharp)
- [导入]asp.net 把datatable的数据导出到EXCEL
- asp.net mvc 数据导出excel表及自excel表导入数据到相应表
- ASP.Net MVC利用NPOI导入导出Excel - RuleLu
- asp.net导出/导入规则的Excel(.xls)文件
- Asp.net程序中用NPOI生成标准Excel报表,导入导出一应俱全[转]
- asp.net Mvc Npoi 导出导入 excel