• 导出含有图片的EXCEL的解决办法


    导出EXCEL的是我们程序中常有的操作,而平时操作的都是只有文本数据的EXCEL,很少会导出图片到EXCEL中。导出图片到EXCEL,可以利用EXCEL.DLL操作EXCEL,然而,如果服务器上要求不安装OFFICE的话,这个时候就很难处理(有时候确实有这样的需求,服务器上不让装OFFICE)。经过考虑,我用了一种较为另类的解决办法:

    1、把数据写成一个HTML的表格形式到一个txt文本文件中。

    2、表格显示图片的单元格中存储图片的路径。

    3、新建一个文件夹存放路径

    4、强制性更改文本文件为XLS后缀。

    这样到处的文件将有一个图片文件夹+一个EXCLE文件。

    Code
      int ICardInfoService.ExportDataToExcelFile(string filePath, DateTime beginTime, DateTime endTime)
            {
                
    try
                {
                    DataSet ds 
    = dao.GetDataToExcel(beginTime, endTime);//得到要到处的数据,最后一列存储的是图片路径
                    StringBuilder sb = new StringBuilder();
                    
    //StreamWriter streamw = File.CreateText(filePath); 
                    StreamWriter streamw = new StreamWriter(filePath, false, System.Text.Encoding.GetEncoding("GB2312"));//解决乱码问题
                    sb.Append("<table style=\"border: 1px solid #000000;\">");
                    
    if (ds != null)
                    {
                        
    for (int m = 0; m < ds.Tables[0].Columns.Count; m++)
                        {
                            sb.Append(
    "<th style=\"border: 1px solid #000000;\">");
                            sb.Append(ds.Tables[
    0].Columns[m].ColumnName);
                            sb.Append(
    "</th>");
                        }
    //循环写标题

                        
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            sb.Append(
    "<tr style=\"height:160px;\">");
                            
    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                            {
                               
                                
    if (j != ds.Tables[0].Columns.Count-1)
                                {
                                    sb.Append(
    "<td style=\"border: 1px solid #000000;\">");
                                    sb.Append(ds.Tables[
    0].Rows[i][j].ToString());
                                    sb.Append(
    "</td>");
                                }
                                
    else
                                {
                                    sb.Append(
    "<td style=\"border: 1px solid #000000;250px\">");
                                    
    string newPhotoFile = Environment.CurrentDirectory + "\\ExportFile";
                                    
    string photoPath=Environment.CurrentDirectory+"\\"+ds.Tables[0].Rows[i][j].ToString();
                                    File.Copy(photoPath, newPhotoFile 
    + "\\" + ds.Tables[0].Rows[i][j].ToString(),true);
                                    sb.Append(
    "<img ");
                                    sb.Append(
    "src=\"");
                                    sb.Append(ds.Tables[0].Rows[i][j].ToString());
                                    sb.Append(
    "\"/>");
                                    sb.Append("</td>");
                                }
    //最后一列显示图片,单独处理
                                
                            }
                            sb.Append(
    "</tr>");
                        }
    //循环写数据
                        streamw.Write(sb.ToString());
                        streamw.Close();

                        
    string newpath= Path.ChangeExtension(filePath, ".xls");//强制性更改文件名
                        File.Move(filePath, newpath); 
                        
    return 1;
                    }
                    
    return -1;

                }
                
    catch (Exception e)
                {
                    LogUtil.Error(e);
                    
    return -1;
                }
            }

    调用方法:

    Code
     string newPhotoFile = Environment.CurrentDirectory + "\\ExportFile";
                Directory.CreateDirectory(newPhotoFile 
    + "\\photos");//新建导出目录
                string tempFile = newPhotoFile + "\\temp.txt";//先新建一个文本文件存储HTML
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
                
    int result = this.service.ExportDataToExcelFile(tempFile, beginTime, endtime);
                
    if (result == -1)
                {
                    
    return result;
                }

    客户下载的时候,需要下载整个文件目录,该目录中包括了图片文件夹和一个EXCEL文件。

    没啥技术含量,但却是个不错的思路,不知道放首页上是否合适,呵呵,希望大家不要拍砖:)

  • 相关阅读:
    二分查找
    Android手机APN设置(中国移动 联通3G 电信天翼),解决不能上网的问题
    cocos2dx 3.1从零学习(三)——Touch事件(回调,反向传值)
    Groovy新手教程
    总结一下用caffe跑图片数据的研究流程
    JAVA数组的定义及用法
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
    LeetCode——Reverse Words in a String
    oracle 库文件解决的方法 bad ELF interpreter: No such file or directory
    布局文件提示错误“No orientation specified, and the default is horizontal. This is a common so...”
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1596468.html
Copyright © 2020-2023  润新知