如何在生成的excel中更改标题列颜色
我正在将sql server数据库中的数据导出到wpf中的excel中,并成功地实现了该功能。现在我想在生成的excel中更改标题栏的颜色。有什么想法吗?提前谢谢如何在生成的excel中更改标题列颜色,excel,export-to-excel,Excel,Export To Excel,我正在将sql server数据库中的数据导出到wpf中的excel中,并成功地实现了该功能。现在我想在生成的excel中更改标题栏的颜色。有什么想法吗?提前谢谢 private void button1_Click(object sender, RoutedEventArgs e) { string sql = null; string data = null; // string path = null; //st
private void button1_Click(object sender, RoutedEventArgs e)
{
string sql = null;
string data = null;
// string path = null;
//string myfilename = "Report";
int i = 0;
int j = 0;
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
//xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Name = "Customer List";
//connectionString = "data source=servername;initial catalog=databasename;user id=username;password=password;";
//SqlConnection cnn = new SqlConnection(GetConnectionString());
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=ClientLists;Integrated Security=SSPI;";
cnn.Open();
sql = "select FirstName, LastName, City, PostCode, TelephoneNo from Customers";
SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
DataSet ds = new DataSet();
dscmd.Fill(ds);
for (i = 0; i <= ds.Tables[0].Rows.Count -1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count -1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 2, j + 1] = data;
}
}
Microsoft.Office.Interop.Excel.Range headerRange1 = xlWorkSheet.get_Range("A1", "A1");
headerRange1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange1.Value = "First Name";
headerRange1.Font.Bold = true;
headerRange1.ColumnWidth = 14;
// headerRange1.Interior.Color = 1;
// headerRange1.Borders.Color = System.Drawing.Color.Red;
Microsoft.Office.Interop.Excel.Range headerRange2 = xlWorkSheet.get_Range("B1", "B1");
headerRange2.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange2.Value = "Last Name";
headerRange2.Font.Bold = true;
headerRange2.ColumnWidth = 14;
Microsoft.Office.Interop.Excel.Range headerRange3 = xlWorkSheet.get_Range("C1", "C1");
headerRange3.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange3.Value = "City";
headerRange3.Font.Bold = true;
headerRange3.ColumnWidth = 14;
Microsoft.Office.Interop.Excel.Range headerRange4 = xlWorkSheet.get_Range("D1", "D1");
headerRange4.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange4.Value = "Post Code";
headerRange4.Font.Bold = true;
headerRange4.ColumnWidth = 14;
Microsoft.Office.Interop.Excel.Range headerRange5 = xlWorkSheet.get_Range("E1", "E1");
headerRange5.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange5.Value = "Telephone NO";
headerRange5.Font.Bold = true;
headerRange5.ColumnWidth = 14;
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
private void按钮1\u单击(对象发送者,路由目标)
{
字符串sql=null;
字符串数据=null;
//字符串路径=null;
//字符串myfilename=“报告”;
int i=0;
int j=0;
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.工作簿;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
对象错误值=System.Reflection.Missing.Value;
//xlApp=新的Microsoft.Office.Interop.Excel.ApplicationClass();
xlApp=新的Microsoft.Office.Interop.Excel.Application();
xlWorkBook=xlApp.Workbooks.Add(错误值);
xlWorkSheet=(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_项(1);
xlWorkSheet.Name=“客户列表”;
//connectionString=“数据源=服务器名;初始目录=数据库名;用户id=用户名;密码=密码;”;
//SqlConnection cnn=新的SqlConnection(GetConnectionString());
SqlConnection cnn=新的SqlConnection();
cnn.ConnectionString=@“数据源=。\sqlexpress;初始目录=客户端列表;集成安全性=SSPI;”;
cnn.Open();
sql=“从客户处选择名字、姓氏、城市、邮政编码、电话号码”;
SqlDataAdapter dscmd=新的SqlDataAdapter(sql,cnn);
数据集ds=新数据集();
dscmd.Fill(ds);
对于(i=0;i有一篇帖子说这是不可能的
但是,StackOverflow上的帖子提供了更改行颜色的代码…您可以根据自己的目的对其进行调整。谢谢您的帮助。我更改了代码,并且可以在上面的代码中更改颜色。但是现在我想添加一个开放式对话,我在google和StackOverflow中进行了大量研究,它不适用于我的应用程序。有什么想法吗?@DanielXu,如果你还有其他问题,请在单独的问题中提问。同时请撤消对该问题的更改,以便其他用户可以从答案中受益,这在你更改了问题文本后毫无意义。如果有进一步的要求,请将答案标记为正确,如果它有助于你解决问题。因为在以前的方法中,我找不到更改颜色的方法,我将代码更改为上面的,现在颜色可以更改。您认为有必要保留以前的代码吗?如果您在此处经常这样做,您可能会被踢出SO。用户在此处提问,其他用户尝试回答这些问题。您不应该更改此代码如果你没有得到你喜欢的答案,请回答问题和标题。如果你想问一个不同的问题,请留下第一个问题,然后再问一个新问题。如果你得到了对你的问题有用的信息,你完全可以接受为你的问题添加更新,但你不应该更改它,特别是不应该完全更改它。如果如果我是你,我会把你的新问题复制到一个新问题上,并撤销你在这里所做的更改。谢谢你告诉我。我会照你说的做。因为我是stackoverflow的新人,对此表示抱歉。