wtblao 2016-05-26 08:47 采纳率: 100%
浏览 1448
已采纳

.NET 导出图片到Excel中,打开不显示(或提示受保护)问题。

public static MemoryStream TablesToExcelForXLSX(DataTable dt, DataTable reportTable, string file, int timeCount, string exportAll, string exportDatasource, string exportChart, out byte[] buf)
{
//HSSFWorkbook
//POIDocument, IWorkbook, IList, ICollection, IEnumerable, IEnumerable

            //POIXMLDocument, IWorkbook, IList<ISheet>, ICollection<ISheet>, IEnumerable<ISheet>, IEnumerable
            HSSFWorkbook xssfworkbook = new HSSFWorkbook();
            string columnName = "";
            int tmpI = 0;
            if (exportAll == "1" || exportDatasource == "1")
            {
                #region chartsource table
                if (dt != null && dt.Rows.Count != 0)
                {
                    //sheet1
                    ISheet sheet = xssfworkbook.CreateSheet("DataSource");
                    //表头
                    IRow row = sheet.CreateRow(0);
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        columnName = dt.Columns[i].ColumnName;
                        if (columnName.ToLower() == "linecount" || columnName.ToLower() == "sort")
                        {
                            continue;
                        }
                        if (columnName.ToLower() == "enlinename")
                        {
                            columnName = "ProductLine";
                        }
                        if (columnName.ToLower() == "thetype")
                        {
                            columnName = "Type";
                        }
                        if (i > timeCount + 3) //只显示时间跨度内的字段,加3 是除时间字段外有3字段
                        {
                            continue;
                        }
                        ICell cell = row.CreateCell(tmpI);
                        cell.SetCellValue(columnName);
                        tmpI++;
                    }

                    //数据
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        tmpI = 0;
                        IRow row1 = sheet.CreateRow(i + 1);
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            if (j == 0 || j == 3)
                            {
                                continue;//过滤每行的lineCount和sort字段
                            }
                            if (j > timeCount + 3) //只显示时间跨度内的字段,加1 是除时间字段外有1字段
                            {
                                continue;
                            }
                            ICell cell = row1.CreateCell(tmpI);
                            var cellValue = dt.Rows[i][j].ToString();
                            if (tmpI == 0)//第一列为产品线名称
                            {
                                cellValue = (cellValue.IndexOf("cn:") == -1) ? cellValue : cellValue.Substring(cellValue.IndexOf("cn:") + 3);
                            }
                            cell.SetCellValue(cellValue);
                            tmpI++;
                        }
                    }
                }
                #endregion

                #region datasource table
                if (reportTable != null && reportTable.Rows.Count != 0)
                {
                    //sheet2
                    ISheet sheet2 = xssfworkbook.CreateSheet("ReportSource");
                    //表头
                    IRow row2 = sheet2.CreateRow(0);
                    tmpI = 0;
                    for (int i = 0; i < reportTable.Columns.Count; i++)
                    {
                        columnName = reportTable.Columns[i].ColumnName;
                        if (columnName.ToLower() == "sort")
                        {
                            continue;
                        }
                        if (columnName.ToLower() == "thetype")
                        {
                            columnName = "Type";
                        }
                        ICell cell = row2.CreateCell(tmpI);
                        cell.SetCellValue(columnName);
                        tmpI++;
                    }

                    //数据
                    for (int k = 0; k < reportTable.Rows.Count; k++)
                    {
                        tmpI = 0;
                        IRow row1 = sheet2.CreateRow(k + 1);
                        for (int j = 0; j < reportTable.Columns.Count; j++)
                        {
                            if (j == 1)
                            {
                                continue;//过滤每行sort字段
                            }
                            if (j > timeCount + 1) //只显示时间跨度内的字段,加1 是除时间字段之前有一个1字段
                            {
                                continue;
                            }
                            ICell cell = row1.CreateCell(tmpI);
                            cell.SetCellValue(reportTable.Rows[k][j].ToString());
                            tmpI++;
                        }
                    }
                }
                #endregion
            }
            if (exportAll == "1" || exportChart == "1")
            {
                #region 插入图片
                string imgpath = AppDomain.CurrentDomain.BaseDirectory + "/export/sellthrough_chart.png";
                if (File.Exists(imgpath))
                {
                    //create sheet
                    //HSSFSheet hssfSheet = (HSSFSheet)xssfworkbook.CreateSheet("Chart");
                    ISheet hssfSheet = xssfworkbook.CreateSheet("Chart");
                    //hssfSheet.Protect = false;
                    // Create the drawing patriarch.  This is the top level container for all shapes. 
                    //HSSFPatriarch patriarch = (HSSFPatriarch)hssfSheet.CreateDrawingPatriarch();
                    IDrawing patriarch = hssfSheet.CreateDrawingPatriarch();

                    //add picture data to this workbook.
                    byte[] bytes = System.IO.File.ReadAllBytes(imgpath);
                    int pictureIdx = xssfworkbook.AddPicture(bytes, PictureType.PNG);

                    //add a picture
                    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 0, 0, 0, 15, 30);
                    //HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
                    IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);
                }
                #endregion
            }
            //转为字节数组
            MemoryStream stream = new MemoryStream();

            xssfworkbook.Write(stream);
            buf = stream.ToArray();
            //var buf = stream.ToArray();
            /*
            //保存为Excel文件
            using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
            {
                fs.Write(buf, 0, buf.Length);
                fs.Flush();
            }*/
            return stream;
        }
  • 写回答

4条回答 默认 最新

  • wtblao 2016-05-26 09:38
    关注

    根据别人意见,换个图片格式测试。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。