如何在创建word doc时动态地将行值与列标题对齐 [英] How to align row values with column headers dynamically while creating word doc

查看:53
本文介绍了如何在创建word doc时动态地将行值与列标题对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我可以获得doc这个词。我无法将行

中的值与列标题对齐。共有7列。该文档似乎是以A4大小生成的。标题显示为2行,值显示在单行中。如何使它看起来一致。

是否可以使用法律或横向大小并使所有内容对齐或如何使用字体大小纠正?

With the following code I am able to get the word doc. I am unable to align the values in line
with the column headers. There are 7 columns. The doc is generated in A4 size it seems. The headers appear in 2 rows and the values appear in single rows. How to make it appear uniformly.
Is it possible to use Legal or landscape size and make everything aligned or how to rectify using font size?

private void btnPrint_Click(object sender, EventArgs e)
{
 //   printDocument1.Print();
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Filter = "Word Documents (*.doc)|*.doc";
    sfd.FileName = "time.doc";
    if (sfd.ShowDialog() == DialogResult.OK)
    {
        //ToCsV(dataGridView1, @"c:\export.xls");
        ToCsV(gvUserInfo, sfd.FileName); // Here dataGridview1 is your grid view name
    }
}

private void ToCsV(DataGridView dGV, string filename)
{
    string stOutput = "";
    // Export titles:
    string sHeaders = "";
    for (int j = 0; j < dGV.Columns.Count; j++)
        
        sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
    
    stOutput += sHeaders + "\r\n";
    // Export data.
    for (int i = 0; i < dGV.RowCount - 1; i++)
    {
        string stLine = "";
        for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
            
            stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
        stOutput += stLine + "\r\n";
    }
    Encoding utf16 = Encoding.GetEncoding(1254);
    byte[] output = utf16.GetBytes(stOutput);
    FileStream fs = new FileStream(filename, FileMode.Create);
    BinaryWriter bw = new BinaryWriter(fs);
    
    bw.Write(output, 0, output.Length); //write the encoded file
    bw.Flush();
    bw.Close();
    fs.Close();
}

推荐答案

这篇关于如何在创建word doc时动态地将行值与列标题对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆