public string CopyToWord() { object filename = @"C:\" + System.DateTime.Now.ToFileTime() + ".doc"; Word._Application application = new Word.ApplicationClass(); Word._Document document = application.Documents.Add(ref missing, ref missing, ref missing, ref missing); foreach (Excel.Worksheet sheet in wbb.Sheets) { if (sheet.Index == wbb.Sheets.Count) { continue; } for (int i = 1; i < sheet.UsedRange.Rows.Count; i++) { Word.Paragraph para = document.Paragraphs.Add(ref missing); string text = ""; for (int j = 1; j < sheet.UsedRange.Columns.Count; j++) { Excel.Range range = (Excel.Range)sheet.Cells[i, j]; if (range.Value2 != null) { text += range.Value2.ToString() + "\t"; if (range.Value2.ToString().StartsWith("#picture")) { string[] value = System.Text.RegularExpressions.Regex.Split(range.Value2.ToString(), "_"); Excel.Shape shape = sheet.Shapes.Item(int.Parse(value[1])) as Excel.Shape; shape.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlBitmap); Word.Paragraph picture = document.Paragraphs.Add(ref missing); picture.Range.Paste(); picture.Format.CharacterUnitFirstLineIndent = 2; picture.Range.InsertParagraphAfter(); text = ""; i += int.Parse(value[2]) - 1; break; } } } para.Range.Text = text; para.Format.CharacterUnitFirstLineIndent = 2; para.Range.InsertParagraphAfter(); } } document.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); document.Close(ref missing, ref missing, ref missing); application.Quit(ref missing, ref missing, ref missing); return(filename.ToString()); }
private void setTitle(int iSCol, int iECol, string sFileTitle) { try { int iBoxWidth = 0; double dWithSum = 0.0; for (int i = iSCol; i <= iECol; i++) { dWithSum += Convert.ToDouble(xlSheet.get_Range(xlSheet.Cells[1, i], xlSheet.Cells[1, i]).ColumnWidth); } //Column항목이 적을 경우 전체 컬럼크기를 키운다. double dTmpVal = 0.0; int iIncVal = 0; int iTmpColCnt = 0; iTmpColCnt = iECol - iSCol + 1; if (dWithSum < 112) { dTmpVal = (112 - dWithSum) / iTmpColCnt; iIncVal = Convert.ToInt16(dTmpVal); for (int i = iSCol; i <= iECol; i++) { xlSheet.get_Range(xlSheet.Cells[1, i], xlSheet.Cells[1, i]).ColumnWidth = Convert.ToDouble(xlSheet.get_Range(xlSheet.Cells[1, i], xlSheet.Cells[1, i]).ColumnWidth) + Convert.ToDouble(iIncVal); } } dWithSum = 0; for (int i = iSCol; i <= iECol; i++) { dWithSum += Convert.ToDouble(xlSheet.get_Range(xlSheet.Cells[1, i], xlSheet.Cells[1, i]).Width); } //iBoxWidth = Convert.ToInt16(dWithSum / 4); if (iBoxWidth < 175) { iBoxWidth = 175; } if (iBoxWidth > 250) { iBoxWidth = 200; } Excel.Shape oTitle = xlSheet.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, iBoxWidth, 5, iBoxWidth * 2, 30); oTitle.Line.DashStyle = Microsoft.Office.Core.MsoLineDashStyle.msoLineSolid; oTitle.Line.Style = Microsoft.Office.Core.MsoLineStyle.msoLineThinThick; oTitle.Line.ForeColor.SchemeColor = 64; oTitle.Shadow.Type = Microsoft.Office.Core.MsoShadowType.msoShadow6; oTitle.Placement = Excel.XlPlacement.xlFreeFloating; //oTitle.TextFrame.Characters(1, sFileTitle.Length).Text = sFileTitle; oTitle.TextFrame.Characters(0, 1).Text = sFileTitle; oTitle.TextFrame.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; oTitle.TextFrame.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; oTitle.TextFrame.Characters(1, sFileTitle.Length).Font.Size = 18; oTitle.TextFrame.Characters(1, sFileTitle.Length).Font.FontStyle = "bold"; oTitle.TextFrame.Characters(1, sFileTitle.Length).Font.Strikethrough = false; } catch (Exception) { //DO Nothing } }