vb.net的例子,供参考: Public Sub DataGridToExcel(ByVal myDataGrid As DataGrid, ByVal FileName As String) Dim xlsheet As New SpreadsheetClass Dim i As Integer = 0 Dim j As Integer = 0 'Response.End() ' 输出标题 Dim oItem As DataGridColumn For Each oItem In myDataGrid.Columns xlsheet.ActiveSheet.Cells(1, i + 1) = oItem.HeaderText 'xlsheet.ActiveSheet.Range(xlsheet.ActiveSheet.Cells(1, 1), xlsheet.ActiveSheet.Cells(1, i + 1)).Font.Bold = True '设置格式 xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Bold = True xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Color = "Black" i = i + 1 Next Dim numbercols As Integer = myDataGrid.Items.Item(0).Cells.Count ' 输出字段内容 For j = 0 To myDataGrid.Items.Count - 1 For i = 0 To numbercols - 1 xlsheet.Range(xlsheet.Cells(2, 2), xlsheet.Cells(j + 2, i + 1)).Font.Color = "blue" 'xlsheet.Range("A2:B14").WrapText = True xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(j + 2, i + 1)).AutoFitColumns() xlsheet.ActiveSheet.Cells(j + 2, i + 1) = myDataGrid.Items.Item(j).Cells(i).Text.Replace(" ", " ") Next Next 'Try ' 'xlsheet.ActiveSheet.Export(Server.MapPath(".") + "\Images\" + Me.xlfile.Text, OWC.SheetExportActionEnum.ssExportActionNone) xlsheet.ActiveSheet.Export(FileName, OWC.SheetExportActionEnum.ssExportActionNone) 'Catch e As System.Runtime.InteropServices.COMException ' Response.Write("错误:" + e.Message) 'End Try End Sub
public void DataGridToExcel(DataGrid myDataGrid, string FileName) { SpreadsheetClass xlsheet = new SpreadsheetClass(); int i = 0; int j = 0; DataGridColumn oItem; foreach (int oItem in myDataGrid.Columns) { xlsheet.ActiveSheet.Cells(1, i + 1) = oItem.HeaderText; xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Bold = true; xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Color = "Black"; i = i + 1; } int numbercols = myDataGrid.Items.Item(0).Cells.Count; for (int j = 0; j <= myDataGrid.Items.Count - 1; j++) { for (int i = 0; i <= numbercols - 1; i++) { xlsheet.Range(xlsheet.Cells(2, 2), xlsheet.Cells(j + 2, i + 1)).Font.Color = "blue"; xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(j + 2, i + 1)).AutoFitColumns(); xlsheet.ActiveSheet.Cells(j + 2, i + 1) = myDataGrid.Items.Item(j).Cells(i).Text.Replace(" ", " "); } } xlsheet.ActiveSheet.Export(FileName, OWC.SheetExportActionEnum.ssExportActionNone); }
用C#生成Excel文件的方法和Excel.dll组件生成的方法
一个示例:class AppTest { private Excel.ApplicationClass _x; public static void Main0() { AppTest a = new AppTest(); a._x = new Excel.ApplicationClass(); a._x.UserControl = false; for (int i = 0 ;i < 4; i++) {
Public Sub DataGridToExcel(ByVal myDataGrid As DataGrid, ByVal FileName As String)
Dim xlsheet As New SpreadsheetClass
Dim i As Integer = 0
Dim j As Integer = 0
'Response.End()
' 输出标题
Dim oItem As DataGridColumn
For Each oItem In myDataGrid.Columns
xlsheet.ActiveSheet.Cells(1, i + 1) = oItem.HeaderText
'xlsheet.ActiveSheet.Range(xlsheet.ActiveSheet.Cells(1, 1), xlsheet.ActiveSheet.Cells(1, i + 1)).Font.Bold = True
'设置格式
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Bold = True
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Color = "Black"
i = i + 1
Next Dim numbercols As Integer = myDataGrid.Items.Item(0).Cells.Count
' 输出字段内容
For j = 0 To myDataGrid.Items.Count - 1
For i = 0 To numbercols - 1
xlsheet.Range(xlsheet.Cells(2, 2), xlsheet.Cells(j + 2, i + 1)).Font.Color = "blue"
'xlsheet.Range("A2:B14").WrapText = True
xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(j + 2, i + 1)).AutoFitColumns()
xlsheet.ActiveSheet.Cells(j + 2, i + 1) = myDataGrid.Items.Item(j).Cells(i).Text.Replace(" ", " ")
Next
Next
'Try
' 'xlsheet.ActiveSheet.Export(Server.MapPath(".") + "\Images\" + Me.xlfile.Text, OWC.SheetExportActionEnum.ssExportActionNone)
xlsheet.ActiveSheet.Export(FileName, OWC.SheetExportActionEnum.ssExportActionNone)
'Catch e As System.Runtime.InteropServices.COMException
' Response.Write("错误:" + e.Message)
'End Try
End Sub
{
SpreadsheetClass xlsheet = new SpreadsheetClass();
int i = 0;
int j = 0;
DataGridColumn oItem;
foreach (int oItem in myDataGrid.Columns) {
xlsheet.ActiveSheet.Cells(1, i + 1) = oItem.HeaderText;
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Bold = true;
xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Color = "Black";
i = i + 1;
}
int numbercols = myDataGrid.Items.Item(0).Cells.Count;
for (int j = 0; j <= myDataGrid.Items.Count - 1; j++) {
for (int i = 0; i <= numbercols - 1; i++) {
xlsheet.Range(xlsheet.Cells(2, 2), xlsheet.Cells(j + 2, i + 1)).Font.Color = "blue";
xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(j + 2, i + 1)).AutoFitColumns();
xlsheet.ActiveSheet.Cells(j + 2, i + 1) = myDataGrid.Items.Item(j).Cells(i).Text.Replace(" ", " ");
}
}
xlsheet.ActiveSheet.Export(FileName, OWC.SheetExportActionEnum.ssExportActionNone);
}
一个示例:class AppTest
{
private Excel.ApplicationClass _x;
public static void Main0()
{
AppTest a = new AppTest();
a._x = new Excel.ApplicationClass();
a._x.UserControl = false;
for (int i = 0 ;i < 4; i++)
{
a.SaveToXls("D:\\test\\" + i + ".xls"); // 本例是在D盘下建立的test文件夹
}
a._x.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject((object) a._x);
System.GC.Collect();
} private void SaveToXls(string filename)
{
Excel.WorkbookClass wb = (Excel.WorkbookClass) this._x.Workbooks.Add(System.Reflection.Missing.Value);
for(int i = 1;i <= 4; i++)
{
this._x.Cells[i,1]=i.ToString();
this._x.Cells[i,2]="'bbb2";
this._x.Cells[i,3]="'ccc3";
this._x.Cells[i,4]="'aaa4";
}
wb.Saved = true;
this._x.ActiveWorkbook.SaveCopyAs(filename);
}
}【注:在VS.Net中运行是要添加Excel.dll组件的,Excel组件VS.Net本身是没有的,下面是生成Excel.dll的方法。】1.要保证机器本身要安装OFFICE. 2.把[C:\Program Files\Microsoft Office\Office:默认安装路径]下的EXCEL9.OLB文件拷贝到[C:\Visual Studio.Net\SDK\v1.1\Bin:VS.Net安装路径]路径下。3.打开Visual Studio .Net2003命令提示,运行TlbImp Excel9.olb Excel.dll ,就会在[C:\Visual Studio.Net\SDK\v1.1\Bin]下生成Excel.dll组件。4.在项目中添加Excel.dll引用就OK了。
这个在2003上行吗?
为什么我运行TlbImp Excel9.olb Excel.dll ,以后,显示已经成功了.为什么在C:\Visual Studio.Net\SDK\v1.1\Bin] 中找不到Excel.dll 啊?
谢谢