|
本帖最后由 leetle 于 2014-3-9 17:13 编辑
NPOI导出多表头操作, 搞不定的加我Q909994561
/// <summary>
/// 通过NPOI,读模版表格,生成报表(针对不规则表格处理)
/// </summary>
/// <param name="fileName">报表名称</param>
/// <param name="context">上下文件</param>
/// <param name="parms">第几行几列存储的内容,格式:1|1</param>
/// <param name="templatePath">模版文件</param>
/// <param name="filePath">生成后的文件位置</param>
/// <returns></returns>
public static bool ExportExcel(string fileName, HttpContext context, //
Dictionary<string, object> parms, string templatePath, out string filePath)
{
InitializeWorkbook(context, templatePath);
var sheet = _hssfworkbook.GetSheetAt(0);
foreach (var o in parms)
{
var key = o.Key;
var value = o.Value.ToString();
var row = Convert.ToInt32(key.Split('|')[0]);
var col = Convert.ToInt32(key.Split('|')[1]);
var dataRow = sheet.GetRow(row);
var newCell = dataRow.GetCell(col);
if (newCell == null)
continue;
newCell.SetCellValue(value);
}
var sequence = SequenceGuid.GetGuid();//new SequenceService().CreateSequence("LH");
var uploadpath = ConfigurationManager.AppSettings["上传文件路径"];
//该目录设定死,最好不要修改
var fileTemp = (string.Format(@"{0}/temp/{1}/", uploadpath, DateTime.Now.ToString("yyyy-MM-dd"))); //
if (!LHDirFile.XFileExists(context.Server.MapPath(fileTemp)))
{
LHDirFile.XCreateDir(context.Server.MapPath(fileTemp));
}
//
uploadpath = string.Format(@"{0}/{1}{2}.xls", fileTemp, fileName, sequence);
//创建文件
using (var fileStream = new FileStream(context.Server.MapPath(uploadpath), FileMode.Create))
{
_hssfworkbook.Write(fileStream);
fileStream.Close();
}
filePath = uploadpath;
return true;
}
调用过程
====================================
string filePath;
var data = SHelper.Search(string.Format(@"select name,cardno,sex,
birthday=CONVERT(varchar(6), birthday, 112),
insurance_begindate=CONVERT(varchar(6), insurance_begindate, 112),
用工形式='合同',在岗状况='在岗',婚姻状况='',生育状况='',月工资=declare_base,通讯地址='建设西路11号',联系电话='55199320',变更原因=''
from [dbo].[ss_member]
where deleteflag=0
and id in ({0})", ids));
var parms = new Dictionary<string, object>();
parms.Clear();
if (data != null && data.Tables.Count > 0&&data.Tables[0].Rows.Count>0)
{
parms.Add("3|1", data.Tables[0].Rows[0]["name"]);
parms.Add("3|3", data.Tables[0].Rows[0]["cardno"]);
parms.Add("3|5", data.Tables[0].Rows[0]["sex"]);
parms.Add("3|8", data.Tables[0].Rows[0]["insurance_begindate"]);
NpoiUtility.ExportExcel("LH", Context, parms, Constant.YlbxContinue, out filePath);
Response.Redirect(string.Format(@"{0}", filePath), false);
}
}
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|