1:在服务器上安装office的Excel软件.
2:在"开始"->"运行"中输入dcomcnfg.exe启动"组件服务"
3:依次双击"组件服务"->"计算机"->"我的电脑"->"DCOM配置"
4:在"DCOM配置"中找到"Microsoft
Excel
应用程序",在它上面点击右键,然后点击"属性",弹出"Microsoft
Excel
应
用程序属性"对话框
5:点击"标识"标签,选择"交互式用户"
6:点击"安全"标签,在"启动和激活权限"上点击"自定义",然后点击对应的"编辑"按钮,在弹出的"安全性"对话框中填加
一个"NETWORK
SERVICE"用户(注意要选择本计算机名),并给它赋予"本地启动"和"本地激活"权限.
7:依然是"安全"标签,在"访问权限"上点击"自定义",然后点击"编辑",在弹出的"安全性"对话框中也填加一个"NETWORK
SERVICE"用户,然后赋予"本地访问"权限.
这样,我们便配置好了相应的Excel的DCOM权限.
注意:这是在WIN2003上配置的,在2000,xp上,是配置ASPNET用户
如果上述方法不能解决(windows
2003)环境里的问题。可以用如下方法:
方法二:
1、创建一个用户账号,设置所属于Power
User或者User(本地系统账号)
2、运行DCOMCNFG,在Dcom组件里面找到Microsoft
Excel
应用程序或者Microsoft
excel文档-属性-标识里面选择“下列用户”,然后输入前面建立的帐号和密码就可以了;
这里提供两种可导出Excel的方法,一种是直接通过代码编译,定义excel数据,然后导出;一种是通过data table导出成Excel文件。两种方法中需要在VS中引用spire.xls.dll。下面是具体代码示例,以供参考。
方法1:创建Excel然后导出。
using Spire.Xls
using System.IO
namespace CreateExcelFiles
{
class Program
{
static void Main(string[] args)
{
//A: Dynamically create Excel file and save it to stream
Workbook wbToStream = new Workbook()
Worksheet sheet = wbToStream.Worksheets[0]
sheet.Range["C10"].Text = "The sample demonstrates how to save an Excel workbook to stream."
FileStream file_stream = new FileStream("To_stream.xls", FileMode.Create)
wbToStream.SaveToStream(file_stream)
file_stream.Close()
System.Diagnostics.Process.Start("To_stream.xls")
//B. Load Excel file from stream
Workbook wbFromStream = new Workbook()
FileStream fileStream = File.OpenRead("sample.xls")
fileStream.Seek(0, SeekOrigin.Begin)
wbFromStream.LoadFromStream(fileStream)
wbFromStream.SaveToFile("From_stream.xls", ExcelVersion.Version97to2003)
fileStream.Dispose()
System.Diagnostics.Process.Start("From_stream.xls")
}
}
}
方法2:从data table 导出Excel
private void btnRun_Click(object sender, System.EventArgs e)
{
Workbook workbook = new Workbook()
//Initialize worksheet
Worksheet sheet = workbook.Worksheets[0]
sheet.InsertDataTable((DataTable)this.dataGrid1.DataSource,true,2,1,-1,-1)
//Sets body style
CellStyle oddStyle = workbook.Styles.Add("oddStyle")
oddStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin
oddStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin
oddStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin
oddStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin
oddStyle.KnownColor = ExcelColors.LightGreen1
CellStyle evenStyle = workbook.Styles.Add("evenStyle")
evenStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin
evenStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin
evenStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin
evenStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin
evenStyle.KnownColor = ExcelColors.LightTurquoise
foreach( CellRange range in sheet.AllocatedRange.Rows)
{
if (range.Row % 2 == 0)
range.CellStyleName = evenStyle.Name
else
range.CellStyleName = oddStyle.Name
}
//Sets header style
CellStyle styleHeader = sheet.Rows[0].Style
styleHeader.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin
styleHeader.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin
styleHeader.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin
styleHeader.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin
styleHeader.VerticalAlignment = VerticalAlignType.Center
styleHeader.KnownColor = ExcelColors.Green
styleHeader.Font.KnownColor = ExcelColors.White
styleHeader.Font.IsBold = true
sheet.Columns[sheet.AllocatedRange.LastColumn - 1].Style.NumberFormat = "\"$\"#,##0"
sheet.Columns[sheet.AllocatedRange.LastColumn - 2].Style.NumberFormat = "\"$\"#,##0"
sheet.AllocatedRange.AutoFitColumns()
sheet.AllocatedRange.AutoFitRows()
sheet.Rows[0].RowHeight = 20
workbook.SaveToFile("sample.xls")
ExcelDocViewer( workbook.FileName )
}
private void Form1_Load(object sender, System.EventArgs e)
{
Workbook workbook = new Workbook()
workbook.LoadFromFile(@"..\..\..\..\..\..\Data\DataTableSample.xls")
//Initialize worksheet
Worksheet sheet = workbook.Worksheets[0]
this.dataGrid1.DataSource = sheet.ExportDataTable()
}
private void ExcelDocViewer( string fileName )
{
try
{
System.Diagnostics.Process.Start(fileName)
}
catch{}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)