用vs2003和office2002生成excel文件,并且控制当中某一列的对齐方式

来源:百度知道 编辑:UC知道 时间:2024/05/17 12:53:07
我用vs2003生成一个excel文件,这个没有问题了,但是如何来控制当中某一列的对齐方式,注意我用的是vs2003和office2002,我把我的代码写出来,你们看看该怎么办?拜托了,谢谢。
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter( stringWriter );
DataGrid excelPrt = new DataGrid();

System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();
System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();
System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();
// AlternatingStyle.BackColor = System.Drawing.Color.LightGray;
// headerStyle.BackColor =System.Drawing.Color.LightGray;
headerStyle.Font.Bold = true;
// itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;

Response.Clear();
Response.Buffer= true;
Response.Charset="Shift_JI

用这种方式比较方便
之后你就可以用你最后面得代码了。
// Excel.Worksheet xlSheet =null;
// xlSheet.get_Range(xlSheet.Cells[4,5],xlSheet.Cells[4,5]).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
我用的是2008,office2003,在工程里面先添加引用,浏览com,之后找到microsoft excel组建。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Excel.Application app = new ApplicationClass();
app.Visible = true;
//wbk=app.Workbooks.Add(Missing.Value);//新建文件的代码
Excel.Workbook wbk = app.Workbooks.Open(@"c:\Book1.xls", System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.T