EPPLUS 导出excel,包括文本,图片

string[] ids = this.dDataList.SelectedIds;
            if (ids.Length != 1)
            {
                Soyisoft.Web.WebUtil.ShowMessage(this.Page, "请选择一行数据");
                return;
            }

string[] idArr = ids[0].Split(‘-‘);
            if (idArr.Length > 0)
            {
                string pdcr_info_id = idArr[0].ToString();
                string dept_id = idArr[1].ToString();

string url = Server.MapPath(WebCommon.AppRootPath + "/templet/report/plm/Purchase/差异报告.xlsx");
                FileStream file = new FileStream(
                    url,
                    FileMode.Open,
                    FileAccess.Read);

#region Excute the Epplus
                try
                {
                    #region Using ExcelPackage
                    // use the Excel package
                    using (ExcelPackage package = new ExcelPackage(file))
                    {
                        ExcelWorksheet sheet = package.Workbook.Worksheets[1];
                        ExcelWorksheet sheet2 = package.Workbook.Worksheets[2];

#region 主信息
                        DataTable dtPdcr = GetPdcrInfo(pdcr_info_id, dept_id);
                        if (dtPdcr != null && dtPdcr.Rows != null && dtPdcr.Rows.Count > 0)
                        {
                            sheet.Cells["B3"].Value = dtPdcr.Rows[0]["reply_user"].ToString();//批复人
                            sheet.Cells["F3"].Value = dtPdcr.Rows[0]["create_user"].ToString();//创建人
                            sheet.Cells["F5"].Value = dtPdcr.Rows[0]["dept_name_full_cn"].ToString();//公司名称
                            sheet.Cells["F6"].Value = dtPdcr.Rows[0]["address"].ToString();//公司地址
                            sheet.Cells["F7"].Value = dtPdcr.Rows[0]["post"].ToString();//邮编
                            sheet.Cells["F8"].Value = dtPdcr.Rows[0]["tel"].ToString();//电话
                            sheet.Cells["F9"].Value = dtPdcr.Rows[0]["fax"].ToString(); ;//传真

string applyDate = dtPdcr.Rows[0]["pdcr_date"].ToString();//差异登记日期;
                            if (applyDate != null && applyDate.Trim() != "")
                            {
                                sheet.Cells["B14"].Value = DateTime.Parse(applyDate).ToString("yyyy-MM-dd");//差异登记日期
                            }
                            sheet.Cells["B15"].Value = dtPdcr.Rows[0]["dept_no"].ToString(); ;//代理商编码
                        }

sheet.Cells["B13"].Value = GetArriveDate(pdcr_info_id, dept_id);//到货日期

#endregion

#region 填充数据
                        DataTable dt = GetPdcrInfoDetail(pdcr_info_id, dept_id);

if (dt != null && dt.Rows != null && dt.Rows.Count > 0)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                int row = i + 18;//从第18行开始
                                if (dt.Rows[i]["prefix_partsNo"] != DBNull.Value)
                                {
                                    sheet.Cells[row, 1].Value = dt.Rows[i]["prefix_partsNo"].ToString();//配件编码
                                }
                                if (dt.Rows[i]["soi_no"] != DBNull.Value)
                                {
                                    sheet.Cells[row, 2].Value = dt.Rows[i]["soi_no"].ToString();//订单号码(soi_no)
                                }
                                if (dt.Rows[i]["pdcr_qty"] != DBNull.Value)
                                {
                                    sheet.Cells[row, 4].Value = dt.Rows[i]["pdcr_qty"].ToString();//差异数量
                                }
                                if (dt.Rows[i]["row_summary"] != DBNull.Value)
                                {
                                    sheet.Cells[row, 5].Value = dt.Rows[i]["row_summary"].ToString();//行金额
                                }
                                if (dt.Rows[i]["pdcr_type"] != DBNull.Value)
                                {
                                    sheet.Cells[row, 6].Value = dt.Rows[i]["pdcr_type"].ToString();//差异类型
                                }
                                if (dt.Rows[i]["back_parts_str"] != DBNull.Value)
                                {
                                    sheet.Cells[row, 7].Value = dt.Rows[i]["back_parts_str"].ToString();//是否退回配件
                                }

}//end for
                        }// end if
                        #endregion

#region 导出图片
                        string path = WebCommon.Upload_path_sv_document;
                        if (!string.IsNullOrEmpty(pdcr_info_id))
                        {
                            string sqlImg = @"select img_url,image_type,image_title,image_name
 from @volvodb_plm.pdcr_info_image where [email protected]_id and [email protected]_info_id";
                            sqlImg = SqlUtil.setValue(sqlImg, "@volvodb_plm", PlmCommon.Database_plm);
                            sqlImg = SqlUtil.setString(sqlImg, "@dept_id", dept_id);
                            sqlImg = SqlUtil.setString(sqlImg, "@pdcr_info_id", pdcr_info_id);

DataTable dtImg = PlmCommon.QueryTable(sqlImg);

if (dtImg != null && dtImg.Rows.Count > 0 && dtImg.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtImg.Rows.Count; i++)
                                {
                                    string imageType = dtImg.Rows[i]["image_type"].ToString();
                                    string fileName = dtImg.Rows[i]["image_name"].ToString();
                                    string imgUrl = dtImg.Rows[i]["img_url"].ToString();
                                    string title = dtImg.Rows[i]["image_title"].ToString();

string imgPath = path + "//" + imgUrl + "//" + fileName;

if (!File.Exists(imgPath)) continue;

System.Drawing.Image image = Bitmap.FromFile(imgPath);

MemoryStream ms = new MemoryStream();
                                    image.Save(ms, ImageFormat.Jpeg);

ExcelPicture pic = sheet2.Drawings.AddPicture("pic" + i.ToString(), image);
                                    pic.SetSize(320, 180);

#region switch
                                    switch (imageType)
                                    {
                                        case "1":
                                            pic.SetPosition(65, 20);
                                            break;
                                        case "2":
                                            pic.SetPosition(65,380);
                                            break;
                                        case "3":
                                            pic.SetPosition(280, 20);
                                            break;
                                        case "4":
                                            pic.SetPosition(280, 380);
                                            break;
                                        case "5":
                                            pic.SetPosition(495, 20);
                                            break;
                                        case "6":
                                            pic.SetPosition(495, 380);
                                            break;
                                    }//end of switch
                                    #endregion

}//end for
                            }// end if
                        }
                        #endregion

string strRExportName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + PurchaseDefine.EXCEL_EXPORT_PDCR_REPORT_EXPORT_NAME;

//Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        Response.AddHeader("content-disposition", "attachment;  filename=" + HttpUtility.UrlEncode(strRExportName, System.Text.Encoding.UTF8));
                        Response.BinaryWrite(package.GetAsByteArray());
                        Response.Flush();
                        Response.End();
                        package.Dispose();
                        GC.Collect();

// PurchaseCommon.ReportExport(this.Page, package, PurchaseDefine.EXCEL_EXPORT_PDCR_REPORT_EXPORT_NAME);
                    } // End of using ExcelPackage
                    #endregion

file.Close();
                    file.Dispose();
                }
                catch (Exception ex)
                {
                    file.Close();
                    file.Dispose();
                    throw new Exception(ex.Message);
                } // End of try
                #endregion
            }

EPPLUS 导出excel,包括文本,图片

时间: 2024-11-03 21:41:23

EPPLUS 导出excel,包括文本,图片的相关文章

C# NPOI导出Excel和EPPlus导出Excel比较

系统中经常会使用导出Excel的功能. 之前使用的是NPOI,但是导出数据行数多就报内存溢出. 最近看到EPPlus可以用来导出Excel,就自己测了下两者导出上的差异. NPIO官网地址:http://npoi.codeplex.com/ EPPlus官网地址:http://epplus.codeplex.com/ 添加NPOI.EPPlus类库dll使用的是NuGet添加. 在类库References右键Manage NuGet Packages...,之后选择添加对应的dll. 测试结果显

NPOI导出excel(带图片)

近期项目中用到Excel导出功能,之前都是用普通的office组件导出的方法,今天尝试用下NPOI,故作此文以备日后查阅. 1.NPOI官网http://npoi.codeplex.com/,下载最新工具包. 2.选择.net版本(例如dotnet4),引用其中的dll. 3.后台代码 using System; using System.Web; using System.Data; using System.Configuration; using System.IO; using Test

C# EPPlus导出EXCEL,并生成Chart表

一  在negut添加EPPlus.dll库文件. 之前有写过直接只用Microsoft.Office.Interop.Excel 导出EXCEL,并生成Chart表,非常耗时,所以找了个EPPlus控件. 二 代码部分 System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Add("序号", typeof(int)); dt.Columns.Add("数据1", typeof(int)

C# 使用Epplus导出Excel [2]:导出动态列数据

上一篇导出excel,是导出已知固定列,有时候我们根本就不知道有几列.列名是什么,因此这些动态列,可以用Dictionary<string,string>接收. 1.实体Student上加上一个字段Dictionarys Student.cs public class Student { public String Name { get; set; } public String Code { get; set; } public Dictionary<string, string>

Asp.net导出Excel/Csv文本格式数据

刚刚开始做Excel相关的项目,所以遇到的问题不管大小都给记录一下 偶然的机会在添加数据的时候全改成了数字,结果输出的时候全自动变成了科学计数法,这是excel的强大功能,能自动识别数字和字符串,太聪明了反而有些麻烦,就像如果输入身份证(18位数字)的话那就不行了.超过了11位呢,下面查了些资料总结一下解决方案: 方法1:  在往excel中添加数据的时候在数据的前面加 单引号,我的是这个方法解决的 如:Sheet.Cells[iRow, iCol] ="'"+ ds.Tables[0

C# 使用Epplus导出Excel [4]:合并指定行

导出的Excel数据,合并指令行并水平垂直居中 原始数据 合并后的数据 代码如下 /// <summary> /// 合并行 /// </summary> /// <param name="sheet"></param> /// <param name="startRowIndex"></param> /// <param name="mergeRowIndexs"&g

Java web项目从eclipse上移动到tomact服务器上,之路径更改,包括显示图片和导出excel

//项目做完之后,在本机电脑运行完全正常,上传图片,显示图片,导出excel,读取excel等功能,没有任何问题,但是,当打成war包放到服务器上时,这些功能全部不能正常使用. 最大的原因就是,本机测试跟服务器上的路径发生了变化. 记录一下,上传图片和显示图片的代码 1.前端页面: <form action="${pageContext.request.contextPath}/UploadWeiXiuServlet" enctype="multipart/form-d

PHP导入导出excel表格图片(转)

写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xml的也用过,csv的就更不用谈了.呵呵.(COM方式不讲了,这种可读的太多了,我也写过利用wps等进行word等的生成之类的文章 )但是在读的时候,只用过一种,具体是什么忘了,要回去翻代码了.基本上导出的文件分为两种:1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已.修改这种文件后再保存,通常会提示你是否要转换成Excel文件.优

导出Excel之Epplus使用教程4(其他设置)

导出Excel之Epplus使用教程1(基本介绍) 导出Excel之Epplus使用教程2(样式设置) 导出Excel之Epplus使用教程3(图表设置) 导出Excel之Epplus使用教程4(其他设置) 1.嵌入VBA代码 首先将vba代码保存成txt文本格式,然后用epplus去调用这个txt文本文件即可,非常简单,当然要想vba调用成功,前提是你的vba代码是没问题的喽! worksheet.CodeModule.Name = "sheet" ; worksheet.CodeM