標籤:style color io os 使用 ar for 檔案 sp
這段時間一直在做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
差不多了以後總結個類庫
Aspose操作Excel和Word