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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么