Aspose操作Excel和Word

这段时间一直在做office报表开发总结一下!Aspose操作遇到的难点.

读取出excel中的图片保存为静态图

        public void ReadPic(string path, string toPath)
        {
            Common com = new Common();
            int count = 1;
            try
            {
                string fileSaveName = String.Empty;
                string savePath = string.Empty;
                int chartNum = sheet.Charts.Count;
                Dictionary<int, Aspose.Cells.Charts.Chart> dic = new Dictionary<int, Aspose.Cells.Charts.Chart>();
                List<int> list = new List<int>();
                for (int i = 0; i < chartNum; i++)
                {
                    Aspose.Cells.Charts.Chart chart = sheet.Charts[i];
                    int Y = chart.ChartObject.Y;
                    list.Add(Y);
                    dic.Add(Y, chart);
                }
                int[] nums = list.ToArray();
                com.InsertSort(nums);
                list = new List<int>(nums);
          
                for (int i = 0; i < list.Count; i++)
                {
                    string name = com.GetFileName(count);
                    savePath = System.IO.Path.Combine(toPath, name);
                    Aspose.Cells.Charts.Chart chart = dic[list[i]];
                    string nme = chart.Name;
                    Stream imgeStream = new FileStream(savePath, FileMode.Create);

                    //设置chart
                    chart.CategoryAxis.TickLabels.Offset = 0;//X轴:坐标文字距离X轴的距离
                    chart.CategoryAxis.TickLabels.TextDirection = TextDirectionType.Context;//设置文字的方向
                    chart.CategoryAxis.HasMultiLevelLabels = true;//X轴:是否允许有多级标签
                    if (chart.CategoryAxis.TickLabels.RotationAngle!=0)
                    {
                        chart.CategoryAxis.TickLabels.RotationAngle = 255;
                    }
                   // chart.CategoryAxis.TickLabels.RotationAngle = 255;//X轴:坐标文字对齐方向(设置X轴文字对齐居中,请将此项设置为0
                    chart.CategoryAxis.MinorTickMark = TickMarkType.Inside;//以饼图为例:图片文字是显示在饼图里面还是外面
                    chart.CategoryAxis.TickLabelPosition = TickLabelPositionType.NextToAxis;
                    chart.ValueAxis.TickLabelPosition = TickLabelPositionType.Low;
                    chart.Legend.Position = LegendPositionType.Bottom;
                    chart.ToImage(imgeStream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图片流保存到格式一致的文件流
                    count += 2;
                    imgeStream.Close();
                }

            }
            catch (Exception ex)
            {
                throw;
            }
        }

注意使用aspose操作读取静态 图片不完全准确很多情况图片生成的不完整。主要是图片中的文字部分。得用CategoryAxis属性调很容易乱所以不推荐用aspose生成图片


读取单元格真实显示的显示的值row是什么我就不解释了Row类

//设置单元格值
model.Content = row[j].StringValue;


读取单元格本身的值

row[j].Value.ToString()



操作excel比较简单想要什么属性样式的可以留言问我


然后是操作word

首先是写图片

  public void WritePic(string path)
        {
            if (path != "" && System.IO.File.Exists(path))
            {
                Shape shape = builder.InsertImage(path, RelativeHorizontalPosition.Page, -1,
              RelativeVerticalPosition.Paragraph, -1, -1, -1, WrapType.Inline);
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中对齐
                shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
                shape.HorizontalAlignment = HorizontalAlignment.Center;
                shape.VerticalAlignment = VerticalAlignment.Center;
            }
        }
</pre><p>这段代码可以插入图片到指定的段落,RelativeVerticalPosition枚举是外表以各种标准定位想成功插入到指定段落必须把焦点移动过去。</p><p><pre name="code" class="csharp">builder.MoveTo(par);

一旦移动焦点就可以插入移动到的段落了


然后是插入表格

  public void WriteTable(WordCellList model)
        {
            Aspose.Words.Tables.Table table = builder.StartTable();

            //填充表格内容:小标初始位不同于数组的习惯,从1开始,即(2,2)表示第2行第2列  
            for (int i = 0; i < model.RowNum; i++)
            {
                //有的行比较短,不管它,其他格子自动填充为空
                int temp = model.ModelList[i].Count;
                // builder.RowFormat.HeightRule = HeightRule.Exactly;
                builder.RowFormat.Height = 20;
                builder.RowFormat.HeightRule = HeightRule.Exactly;
                ParagraphAlignment paragraphAlignmentValue = builder.ParagraphFormat.Alignment;
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                for (int j = 0; j < temp; j++)
                {
                    WordCell data = model.ModelList[i][j];
                    Cell cell = builder.InsertCell();
                    builder.CellFormat.Width = data.Width * 8;
                    builder.CellFormat.Borders[BorderType.Top].LineStyle = SetBorderStyle(d => builder.CellFormat.Borders[d].LineStyle, BorderType.Top, data.TopBorderStyle);
                    builder.CellFormat.Borders[BorderType.Left].LineStyle = SetBorderStyle(d => builder.CellFormat.Borders[d].LineStyle, BorderType.Left, data.LeftBorderStyle);
                    builder.CellFormat.Borders[BorderType.Right].LineStyle = SetBorderStyle(d => builder.CellFormat.Borders[d].LineStyle, BorderType.Right, data.RightBorderStyle);
                    builder.CellFormat.Borders[BorderType.Bottom].LineStyle = SetBorderStyle(d => builder.CellFormat.Borders[d].LineStyle, BorderType.Bottom, data.BottomBorderStyle);
                    builder.CellFormat.Borders[BorderType.Top].Color = data.TopBorderColor;
                    builder.CellFormat.Borders[BorderType.Left].Color = data.LeftBorderColor;
                    builder.CellFormat.Borders[BorderType.Right].Color = data.RightBorderColor;
                    builder.CellFormat.Borders[BorderType.Bottom].Color = data.BottomBorderColor;
                    builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
                    builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;//垂直居中对齐
                    builder.CellFormat.Shading.BackgroundPatternColor = data.BackGround;
                    #region 字体样式映射
                    builder.Font.Color = data.Font.FontColor;
                    builder.Font.Size = data.Font.FontHeight;
                    builder.Font.Name = data.Font.FontName;
                    builder.Font.Bold = data.Font.FontBold;
                    #endregion
                    builder.Write(data.Content);
                }
                if (i != model.RowNum)
                {
                    builder.EndRow();
                }
                table.Alignment = TableAlignment.Center;
                //根据宽度自动填充
                table.AutoFit(AutoFitBehavior.AutoFitToWindow);
            }
            builder.EndTable();
            //此时当前段落为空行去除即可
            builder.CurrentParagraph.Remove();
        }
  private LineStyle SetBorderStyle(Func<BorderType, LineStyle> func, BorderType type, BorderStyle style)
        {
            LineStyle lineStyle = func(type);
            switch (style)
            {
                case BorderStyle.None:
                    lineStyle = LineStyle.None;
                    break;
                case BorderStyle.Thin:
                    lineStyle = LineStyle.Single;
                    break;
                case BorderStyle.Medium:
                    lineStyle = LineStyle.Single;
                    break;
                case BorderStyle.Dashed:
                    break;
                case BorderStyle.Dotted:
                    lineStyle = LineStyle.Dot;
                    break;
                case BorderStyle.Thick:
                    lineStyle = LineStyle.Thick;
                    break;
                case BorderStyle.Double:
                    lineStyle = LineStyle.Double;
                    break;
                case BorderStyle.Hair:
                    lineStyle = LineStyle.Hairline;
                    break;
                case BorderStyle.MediumDashed:
                    lineStyle = LineStyle.Single;
                    break;
                case BorderStyle.DashDot:
                    lineStyle = LineStyle.DotDash;
                    break;
                case BorderStyle.MediumDashDot:
                    break;
                case BorderStyle.DashDotDot:
                    break;
                case BorderStyle.MediumDashDotDot:
                    break;
                case BorderStyle.SlantedDashDot:
                    break;
                default:
                    lineStyle = LineStyle.None;
                    break;
            }
            return lineStyle;
        }

table.AutoFit比较关键 你表格单元格宽度以什么基准单元格呈现。

要注意一点就是表格插入完毕会多一个段落 想不空行删除即可

然后是替换

 range.Replace("&" + str[i + 2], result, false, false);
注意不能替换\r\n

差不多了以后总结个类库


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【核心代码】 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 using Aspose.Cells; using Aspose.Slides.Pptx; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Web.Http; namespace DocOnlineView.UI.Controllers.MVCAPI { public class HomeController : ApiController { [HttpGet] public DataTable CourseViewOnLine(string fileName) { DataTable dtlist = new DataTable(); dtlist.Columns.Add("TempDocHtml", typeof(string)); string fileDire = "/Files"; string sourceDoc = Path.Combine(fileDire, fileName); string saveDoc = ""; string docExtendName = System.IO.Path.GetExtension(sourceDoc).ToLower(); bool result = false; if (docExtendName == ".pdf") { //pdf模板文件 string tempFile = Path.Combine(fileDire, "temppdf.html"); saveDoc = Path.Combine(fileDire, "viewFiles/onlinepdf.html"); result = PdfToHtml( sourceDoc, System.Web.HttpContext.Current.Server.MapPath(tempFile), System.Web.HttpContext.Current.Server.MapPath(saveDoc)); } else { saveDoc = Path.Combine(fileDire, "viewFiles/onlineview.html"); result = OfficeDocumentToHtml( System.Web.HttpContext.Current.Server.MapPath(sourceDoc), System.Web.HttpContext.Current.Server.MapPath(saveDoc)); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值