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

差不多了以后总结个类库

时间: 2024-08-24 13:48:49

Aspose操作Excel和Word的相关文章

针对每种Windows Server 操作Excel、Word等Office组件遇到“ComException&quot;、”80070005“等COM错误的解决方案大汇总

以下所有Excel错误的解决方案,同样适用于Word.PowerPoint等Office产品. 以下解决方案中,如果出现"安装Excel组件",是适用于遇到Excel错误的.如果是Word错误,则安装Word组件,而不是Excel组件,PowerPoint亦是如此,依次类推. 当然,如果你足够任性,你可以选择安装整个Office套件,不会有人说你什么的. 错误: 1.检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的

Visual Basic 2017 操作Excel和word【1】

 清单1.1  从应用程序对象导航到Excel中的工作表  Dim myWorkbooks As Excel.Workbooks = app.Workbooks Dim myWorkbook As Excel.Workbook = myWorkbooks.Item(1) Dim myWorksheets As Excel.Sheets = myWorkbook.Worksheets Dim myWorksheet As Excel.Worksheet myWorksheet = CType(my

Visual Basic 2017 操作Excel和word【2】持续更新……

清单2.1   通过控制台应用程序自动化Excel Imports Excel = Microsoft.Office.Interop.Excel Module Module1 Private exitXL As Boolean = False Dim WithEvents myExcelApp As Excel.Application Sub Main() myExcelApp = New Excel.Application myExcelApp.Visible = True myExcelAp

【C#】解析C#操作Excel表

目录结构: contents structure [+] Microsoft.Office.Interop.Excel.Application Aspose.cell插件 1.Microsoft.Office.Interop.Excel.Application 这.net 库自带的处理Excel的API,不过想要使用该类,电脑上必须已经安装上了Microsoft Office. static void Main(string[] args) { //创建Application的对象 Micros

Windows7与Window2008 64位IIS7上面DCOM配置Excel、Word等

解决办法:1.(适用于.NET) 1).通过webconfig中增加模拟,加入管理员权限, <identity impersonate="true" userName="系统管理员" password="系统管理员密码"/> 2).这样就能够启动Application进程,操作EXCEL了,能够新建EXCEL,导出EXCEL,但是还是不能打开服务器端的EXCEL文件 2.(以下适用于所有语言)  在组件服务,DOCM设置 Micros

JAVA的POI操作Excel

1.1Excel简介 一个excel文件就是一个工作簿workbook,一个工作簿中可以创建多张工作表sheet,而一个工作表中包含多个单元格Cell,这些单元格都是由列(Column)行(Row)组成,列用大写英文字母表示,从A开始到Z共26列,然后再从AA到AZ又26列,再从BA到BZ再26列以此类推.行则使用数字表示,例如:A3 表示第三行第一列,E5表示第五行第五列. 1.2 POI工具包 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Exce

POI操作EXCEL(二)

原文转自:http://www.tqcto.com/article/code/295025.html java当初把核心处理设成Unicode,带来的好处是另代码适应了多语言环境.然而由于老外的英语只有26个字母,有些情况下,一些程序员用8 位的byte处理,一不小心就去掉了CJK的高位.或者是由于习惯在程序中采用硬编码,还有多种原因,使得许多java应用在CJK的处理上很烦恼.还好 在POI HSSF中考虑到这个问题,可以设置encoding为双字节. POI可以到www.apache.org

(一)JAVA使用POI操作excel

1,Poi 简介 Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能 POI为“Poor Obfuscationmplementation”的首字母缩写,意为“可怜的模糊实现”. Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API.用它可以使用Java读取和创建,修改M

VSTO:使用C#开发Excel、Word【9】

文件背后的代码VSTO支持文档背后的代码,要求开发人员使用VSTO项目中生成的具有预连接上下文和预连接事件的类.这些类有时被称为"代码后面"类,因为它们是与特定文档或工作表相关联的代码.在Word中,与文档对应的类后面有一个代码.在Excel中,工作簿中的classone有多个代码,工作簿中的每个工作表或图表工作表都有一个代码. 您的代码在文档项目后面的VSTO代码中第一次运行时,当Office引发由为您创建的类后面的任何代码处理的Startup事件. VSTO通过您正在编写代码的类的