繁体   English   中英

使用C#和ADO OLEDB连接字符串创建一个Excel文件。 如何在Excel粗体显示标题行?

[英]Using C# and ADO OLEDB connection string to create an excel file. How do i make the header row in excel bold?

我将连接字符串设置如下:-

“ Provider = Microsoft.ACE.OLEDB.12.0; Data Source =” + saveFilenameAndLocation +“;扩展属性='Excel 12.0 xml; HDR =是'”

我已经指定第一行是标题,并且我的excel电子表格是使用标题行创建的,其次是所有数据行。 但是,我想使标题行变为粗体,我该怎么做? 任何帮助表示赞赏。

您需要使用Office Interop。 ADO无法做到这一点。

通过在解决方案资源管理器中右键单击“引用”,然后单击“添加引用”,将对项目的引用添加到Microsoft.Office.Interop.Excel。 然后选择“ Microsoft.Office.Interop.Excel”。

这是一个非常简单的示例,它打开一个Excel文档,并在您单击用户表单上的按钮时使第一行变为粗体。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication1
{


    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String filename = "C:\\Path\\To\\Excel\\File\\file.xls";

            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlApp.Visible = true;
            Workbook xlWkb = xlApp.Workbooks.Open(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Worksheet xlSh = xlWkb.Sheets[1] as Worksheet;
            Range xlRng = xlSh.UsedRange.get_Range("A1", Type.Missing).EntireRow;
            xlRng.Font.Bold = true;
            xlWkb.Save();
            xlApp.Quit();

            xlRng = null;
            xlSh = null;
            xlWkb = null;
            xlApp = null;
        }
    }
}

OLEDB仅提供对数据的访问,而不提供格式访问。

要访问单元属性等,您需要使用Interop( http://msdn.microsoft.com/en-us/library/ms173186%28v=vs.80%29.aspx )或Spire等第三方组件.xls( http://www.e-iceblue.com/Introduce/excel-for-net-introduce.html ,商业版)或其他类似选项之一(选中Import and Export Excel-最好的库是什么? ) 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM