首页 > 代码库 > Aspose操作Excel和Word
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 = http://www.mamicode.com/builder.ParagraphFormat.Alignment;>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差不多了以后总结个类库
Aspose操作Excel和Word
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。