Duyuruyu Kapat
Facebook Gözat
Twitter Gözat

c# word dosyası oluşturma

Konu, 'C#' kısmında hgedek tarafından paylaşıldı.

  1. hgedek

    hgedek Üye

    Kayıt:
    20 Aralık 2006
    Mesajlar:
    170
    Beğenilen Mesajlar:
    0
    Şehir:
    Kocaeli
    c# kodu içerisinden word dosyası oluşturmak istiyorum.dosyada tablolar olacak.

    anlamadığım range olayı.yani tabloyu word.document nesnesi üzerinde oluşturuyosunuz ama dosyayı bir türlü oluşturamadım.
    ancak word.application kullanınca oluşuyor.nasıl olacak?
     
  2. senol01

    senol01 Üye

    Kayıt:
    12 Haziran 2008
    Mesajlar:
    58
    Beğenilen Mesajlar:
    0
    al kardeşim
    if (Request.QueryString["word"] == "1")
    {
    string strBody;
    strBody = "" +
    "<html xmlns:eek:='urn:schemas-microsoft-com:eek:ffice:eek:ffice' " +
    "xmlns:w='urn:schemas-microsoft-com:eek:ffice:word'" +
    "xmlns='http://www.w3.org/TR/REC-html40'" +
    "><head><title>Time</title>";
    strBody = strBody +
    "<!--[if gte mso 9]><xml>" +
    [COLOR=#a31515][COLOR=#a31515]"<w:WordDocument> <w:View>" +
    [COLOR=#a31515][COLOR=#a31515]"Print</w:View> <BR>" +
    [COLOR=#a31515][COLOR=#a31515]"<w:Zoom>75</w:Zoom> " +
    [COLOR=#a31515][COLOR=#a31515]"<w:DoNotOptimizeForBrowser/>" +
    [COLOR=#a31515][COLOR=#a31515]"</w:WordDocument></xml><![endif]-->";
    strBody = strBody +
    [COLOR=#a31515][COLOR=#a31515]"<style><!-- /* Style Definitions" +
    [COLOR=#a31515][COLOR=#a31515]" */@page Section1{size:15in 11.0in;" +
    [COLOR=#a31515][COLOR=#a31515]"margin:1.0in 1.25in 1.0in " +
    [COLOR=#a31515][COLOR=#a31515]"1.25in;mso-header-margin:.5in; " +
    [COLOR=#a31515][COLOR=#a31515]"mso-footer-margin:.5in; mso-paper-source:0;}";
    strBody = strBody +
    [COLOR=#a31515][COLOR=#a31515]"div.Section1{page:Section1;}--></style></head>";
    strBody = strBody +
    [COLOR=#a31515][COLOR=#a31515]"@page Section1{size:8.5in 11.0in; " +
    [COLOR=#a31515][COLOR=#a31515]"margin:1.0in 1.0in 45.0pt 1.0in;" +
    [COLOR=#a31515][COLOR=#a31515]"mso-header-margin:.5in; " +
    [COLOR=#a31515][COLOR=#a31515]"mso-footer-margin:.5in; mso-paper-source:0;}";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"div.Section1 {page:Section1;}--></style>";
    strBody = strBody +
    [COLOR=#a31515][COLOR=#a31515]"<body lang=TUR style='tab-interval:.5in'><div " +
    [COLOR=#a31515][COLOR=#a31515]"class=Section1><h1>FİYAT TALEBİ RAPORLAMA</h1>" +
    [COLOR=#a31515][COLOR=#a31515]"<p style='color:red'><I>" + [COLOR=#2b91af][COLOR=#2b91af]DateTime.Now +
    [COLOR=#a31515][COLOR=#a31515]"</I></p>";

    DataView dv = see_records_counts.Toplam_kayitlar([COLOR=#a31515][COLOR=#a31515]"select * from tbl_fiyat_bilgisi_istiyorum WHERE fiyat_id=" + Request.QueryString[[COLOR=#a31515][COLOR=#a31515]"fiyat_id"] + [COLOR=#a31515][COLOR=#a31515]"");
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<table border=1>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<tr>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>ADI SOYADI</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>EMAIL</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>TELEFON</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>FAX</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>NOTU</td>";

    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>KAYIT TARİHİ</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>ŞİRKET</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>YÖNETİCİ NOT</td>";

    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"</tr>";
    [COLOR=#0000ff][COLOR=#0000ff]int i = 0;
    [COLOR=#0000ff][COLOR=#0000ff]for (i = 0; i < dv.Count; i++)
    {
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<tr>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>" + ControlOfTurkishChar(dv[i][[COLOR=#a31515][COLOR=#a31515]"lbl_fiyat_ad_soyad"].ToString()) + [COLOR=#a31515][COLOR=#a31515]"</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>" + ControlOfTurkishChar(dv[i][[COLOR=#a31515][COLOR=#a31515]"lbl_fiyat_email"].ToString()) + [COLOR=#a31515][COLOR=#a31515]"</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>" + ControlOfTurkishChar(dv[i][[COLOR=#a31515][COLOR=#a31515]"lbl_fiyat_telefon"].ToString()) + [COLOR=#a31515][COLOR=#a31515]"</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>" + ControlOfTurkishChar(dv[i][[COLOR=#a31515][COLOR=#a31515]"lbl_fiya_faks"].ToString()) + [COLOR=#a31515][COLOR=#a31515]"</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>" + ControlOfTurkishChar(dv[i][[COLOR=#a31515][COLOR=#a31515]"lbl_fiyat_bilgisi"].ToString()) + [COLOR=#a31515][COLOR=#a31515]"</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>" + ControlOfTurkishChar(dv[i][[COLOR=#a31515][COLOR=#a31515]"lbl_fiyat_kayit_tarih"].ToString()) + [COLOR=#a31515][COLOR=#a31515]"</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>" + ControlOfTurkishChar(dv[i][[COLOR=#a31515][COLOR=#a31515]"lbl_fiyat_geldi_yer"].ToString()) + [COLOR=#a31515][COLOR=#a31515]"</td>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"<td>" + ControlOfTurkishChar(dv[i][[COLOR=#a31515][COLOR=#a31515]"lbl_fiyat_yonetici_not"].ToString()) + [COLOR=#a31515][COLOR=#a31515]"</td>";

    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"</tr>";
    }
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"</table>";
    strBody = strBody + [COLOR=#a31515][COLOR=#a31515]"</div></body></html>";
    Response.AddHeader([COLOR=#a31515][COLOR=#a31515]"Content-Type", [COLOR=#a31515][COLOR=#a31515]"application/msword"); [COLOR=#008000][COLOR=#008000]//msword
    Response.AddHeader([COLOR=#a31515][COLOR=#a31515]"Content-disposition",
    [COLOR=#a31515][COLOR=#a31515]"attachment; filename=Rapor_fiyat_talep.doc");
    Response.Charset = [COLOR=#a31515][COLOR=#a31515]"";
    Response.Write(strBody);

    [COLOR=#0000ff]bu benim siteler
    [URL="http://www.evdenevenakliyatbul.com/"]http://www.evdenevenakliyatbul.com/[/URL]
    [URL="http://www.paslanmazbul.net/"][COLOR=#0066cc]http://www.paslanmazbul.net[/COLOR][/URL][COLOR=#0066cc]

    [/color][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/i][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/i][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/i][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/i][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/i][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/i][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/i][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/i][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]
     
  3. hgedek

    hgedek Üye

    Kayıt:
    20 Aralık 2006
    Mesajlar:
    170
    Beğenilen Mesajlar:
    0
    Şehir:
    Kocaeli
    Benim sormak istediğim bir web uygulaması yada buna benzer bir çalışma üzerinde text sayfası oluşturmak (htm) değildir. C# uygulaması ile doc oluşturmak...
     
  4. hgedek

    hgedek Üye

    Kayıt:
    20 Aralık 2006
    Mesajlar:
    170
    Beğenilen Mesajlar:
    0
    Şehir:
    Kocaeli
    Hazırladığım örnek kod yapısı aşağıda.karşılaştığım sorun ise bir tablo ekledikten sonra 2. si ile arasına nasıl boşluk bırabileceğimdir.

    Kod:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Word=Microsoft.Office.Interop.Word;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace DocCreator
    {
    
        public class DataField
        {
            public DataField()
            {
                Element = new SortedDictionary<long, string>();
            }
            public string Header
            {
                get
                {   
                    return _header;
                }
                set
                {
                    _header=value;
                }
            }
    
            private string _header;
            public SortedDictionary<Int64, String> Element;
            
        }
        class Program
        {
    
            private Word.Application wordApp;
            private Word.Document wordDoc;
            private Word.Range range;
            private Object EndOfDoc;
            public List<DataField> DataFields;
            private object oMissing;
    
            public Program()
            {
                oMissing = System.Reflection.Missing.Value;
                wordApp = new Microsoft.Office.Interop.Word.Application();
                wordDoc = wordApp.Documents.Add(ref oMissing, ref oMissing,ref oMissing, ref oMissing);
                EndOfDoc = "\\endofdoc";
    
                DataFields = new List<DataField>();
            }
            private int SortItems(DataField p1, DataField p2)
            {
                return p1.Element.Count.CompareTo(p2.Element.Count);
            }
    
            public void Sort()
            {
                DataFields.Sort(SortItems);
            }
    
            public void GenerateDocument()
            {
                DrawTable(DataFields);
                GenerateChartResult();
            }
    
            private void DrawTable(List<DataField> fieldList)
            {
                Object defaultTableBehavior = Type.Missing;
                Object autoFitBehavior = false;
                Object row = 1;
                Object column = 2;
                Object none = Type.Missing;
    
                range = wordDoc.Bookmarks.get_Item(ref EndOfDoc).Range;
                
                Word.Table tbl = wordDoc.Tables.Add(range, fieldList[fieldList.Count-1].Element.Count+1, fieldList.Count,
                                    ref defaultTableBehavior, ref autoFitBehavior);
                wordApp.Visible = false;
    
                for (int index = 1; index <= fieldList.Count; index++)
                        tbl.Columns[index].Width = 100;
                for (int index =0; index < fieldList.Count; index++)
                {
                    tbl.Columns[2*index+1].Cells.Split(ref row, ref column, ref none);
                }
    
                for (int index = 1; index <= fieldList.Count; index++)
                    tbl.Rows[1].Cells[index].Merge(tbl.Rows[1].Cells[index + 1]);
    
                FillTable(tbl, fieldList);
                AddInformationText("");
            }
            private void FillTable(Word.Table tbl, List<DataField> fieldList)
            {
    
                int index = 1;
                int rowIndex = 2, colIndex = 1;
                tbl.Range.Font.Size = 8;
                tbl.Range.Font.Name = "Verdana";
    
                Object style = "Table Grid 8";
    
                foreach (DataField field in fieldList)
                {
                    tbl.set_Style(ref style);
                    tbl.Rows[1].Cells[index].Range.Text = "hakana";
                    index++;
                }
    
                foreach (DataField field in fieldList)
                {
                    SortedDictionary<long, String> collection = field.Element;
                    SortedDictionary<long, String>.ValueCollection valueCol = collection.Values;
                    SortedDictionary<long, String>.KeyCollection keyCol = collection.Keys;
    
                    foreach (String s in valueCol)
                    {
                        tbl.Rows[rowIndex].Cells[colIndex].Range.Text = s;
                        rowIndex++;
                    }
    
                    rowIndex = 2;
                    colIndex++;
    
                    foreach (long l in keyCol)
                    {
                        tbl.Rows[rowIndex].Cells[colIndex].Range.Text = l.ToString();
                        rowIndex++;
                    }
    
                    colIndex++;
                    rowIndex = 2;
                }
                //tbl.AllowAutoFit = true;
                //tbl.UpdateAutoFormat(); //font ta değişim oldu
                //tbl.PreferredWidthType = Word.WdPreferredWidthType.wdPreferredWidthPercent; //tüm tabloyu 
                //tbl.PreferredWidth = 100; //bu şekilde boyut olarak değiştirebiliyosun
    
                //tbl.PreferredWidthType = Microsoft.Office.Interop.Word.WdPreferredWidthType.wdPreferredWidthAuto;
                //tbl.PreferredWidth = 10;
            
            
            }
            private void GenerateChartResult()
            {
                Bitmap image, newImg;
                image = (Bitmap) Bitmap.FromFile("C:\\image.jpg");
                range = wordDoc.Bookmarks.get_Item(ref EndOfDoc).Range;
    
               
                if (image.Width > 800 || image.Height > 600)
                {
                    Size size = new Size(800, 600);
                    newImg = new Bitmap(image, size);
                    System.Windows.Forms.Clipboard.SetImage(newImg);
                }
                else
                {
                    System.Windows.Forms.Clipboard.SetImage(image);
                }
                
                range.Paste();
            }
          
            private void AddInformationText(String Text)
            {
    
                Word.Paragraph paraf;
                Object oRng = wordDoc.Bookmarks.get_Item(ref EndOfDoc).Range;
                paraf = wordDoc.Content.Paragraphs.Add(ref oRng);
                paraf.Range.InsertParagraphBefore();
                paraf.Range.Text = Text;
                paraf.Range.InsertParagraphAfter();
    
            }
            [STAThread]
            static void Main(string[] args)
            {
    
                Program program = new Program();
    
                DataField table1 = new DataField();
                DataField table2 = new DataField();
                DataField table3 = new DataField();
                DataField table4 = new DataField();
                DataField table5 = new DataField();
    
                table1.Header = "Tablo1";
                table1.Element.Add(1, "hakan");
                table1.Element.Add(2, "hakan");
                table1.Element.Add(3, "hakan");
    
                table2.Header = "Tablo2";
                table2.Element.Add(1, "hakan");
                table2.Element.Add(2, "hakan");
                table2.Element.Add(3, "hakan");
                table2.Element.Add(4, "hakan");
    
                table3.Header = "Tablo13";
                table3.Element.Add(1, "hakan");
                table3.Element.Add(2, "hakan");
                table3.Element.Add(3, "hakan");
                table3.Element.Add(4, "hakan");
                table3.Element.Add(5, "hakan");
                table3.Element.Add(6, "hakan");
    
                table4.Header = "Tablo4";
                table4.Element.Add(1, "hakan");
                table4.Element.Add(2, "hakan");
                table4.Element.Add(3, "hakan");
    
                table5.Header = "Tablo12";
                table5.Element.Add(1, "hakan");
                table5.Element.Add(2, "hakan");
                table5.Element.Add(3, "hakan");
                table5.Element.Add(4, "hakan");
                table5.Element.Add(5, "hakan");
    
                program.DataFields.Add(table1);
                program.DataFields.Add(table2);
                program.DataFields.Add(table3);
                program.DataFields.Add(table4);
                program.DataFields.Add(table5);
                for (int i = 0; i < 10; i++)
                {
                    program.Sort();
                    program.GenerateDocument();
                }
                program.wordApp.Visible = true;
            }
        }
    }