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

差不多了以后总结个类库


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。