需要有关如何在C#pls帮助中使用excel autofit的示例代码 [英] Need a sample code on how to use the excel autofit in c# pls help

查看:83
本文介绍了需要有关如何在C#pls帮助中使用excel autofit的示例代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,

美好的一天,您能给我一个示例代码来说明如何在C#中使用excel autofit吗?我如何在下面更新当前代码?非常感谢您的帮助. (紧急)感谢,愿上帝保佑.

Hi Sir,

Good day, can you give me a sample code on how could I use the excel autofit in C#? How could I update my current code below? your help really appreciated. (urgent) Thanks and God bless.

private void Convert_TextFile()
{
    // Open the text file in Excel.
    m_objExcel = new Excel.Application();
    m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
    m_objBooks.OpenText(m_strSampleFolder + "test-data.csv",
    //m_objBooks.OpenText(m_strSampleFolder + "mean_act_DL_VISMIN_201102.txt",
        Excel.XlPlatform.xlWindows, 1,
        Excel.XlTextParsingType.xlDelimited,
        Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
        false, true, false, false, false, false, m_objOpt, m_objOpt,
        m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

    m_objBook = m_objExcel.ActiveWorkbook;
    
    // Save the text file in the typical workbook format and quit Excel.
    m_objBook.SaveAs(m_strSampleFolder + "test-data.xls",
    //m_objBook.SaveAs(m_strSampleFolder + "mean_act_DL_VISMIN_201102.xls",
        Excel.XlFileFormat.xlWorkbookNormal,
        m_objOpt, m_objOpt, m_objOpt, m_objOpt,
        Excel.XlSaveAsAccessMode.xlNoChange,
        m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

    m_objBook.Close(false, m_objOpt, m_objOpt);
    m_objExcel.Quit();
    TODO: Code on opening excel in autofit format (entire data)
}

推荐答案

要调整列宽,可以执行以下操作:
To adjust the column width, you can do the following:
Excel.Range xlEntireColumn = null;
Excel.Range xlRange = null;

while (reader.Read()) {
for (int i=0;i<numcols;i++) { 
    xlsheet.ActiveSheet.Cells[row,i+1] = reader.GetValue(i).ToString();

    xlRange = xlSheet.ActiveSheet.Cells[row, i+1];
    xlEntireColumn = xlRange.EntireColumn;
    xlEntireColumn.AutoFit();
}



希望这对您有所帮助.



Hope this might help you.


正如我之前告诉您的那样,我对Excel的使用并不多(为了给您一些想法,我的计算机上的版本是Office''95),但是,我发现了 [ ^ ]线程,该线程似乎包含用于为列设置Excel的Autofit功能的代码.
As I have told you before I do not do very much with Excel (to give you some idea, the version on my machine is Office ''95), however, I have found this[^] thread which seems to include code to set Excel''s Autofit feature for a column.




请参阅此链接...
希望对您有帮助.

导出为Winforms中的Excel [ ^ ]

干杯:)
Hi,

Refer this link...
I hope its really helpful to you.

Export to Excel in Winforms[^]

Cheers :)


这篇关于需要有关如何在C#pls帮助中使用excel autofit的示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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