C# app.net中怎样把dataset中的数据导出到Excel中?

sunxiaoli 2003-10-08 10:11:46
?
...全文
148 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunxiaoli 2003-10-09
  • 打赏
  • 举报
回复
OWC.SpreadsheetClass xlsheet=new OWC.SpreadsheetClass();
string filepath=Server.MapPath("银卡会员注册与消费统计表.xls");
string strFileName="银卡会员注册与消费统计表.xls";
for(int j=0;j<myDt.Columns.Count;j++)
xlsheet.ActiveSheet.Cells[1,j+1]=myDt.Columns[j].ColumnName.ToString();
for(int i=0;i<myDt.Rows.Count;i++)
for(int j=0;j<myDt.Columns.Count;j++)
xlsheet.ActiveSheet.Cells[i+2,j+1]=myDt.Rows[i][j].ToString();
xlsheet.ActiveSheet.Export(filepath,OWC.SheetExportActionEnum.ssExportActionNone);
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(strFileName,System.Text.Encoding.UTF8));
Response.WriteFile(filepath);
Response.End();

用这种方法导出Excel,为什么会提示:线程正被终止!
sunxiaoli 2003-10-08
  • 打赏
  • 举报
回复
感谢大家
to:freecs
strField = document.Excel.txtField.value;
这句话出现找不到对象的提示?
BirdInParadise 2003-10-08
  • 打赏
  • 举报
回复
先引用Microsoft Excel 9.0 Lib,以下代码可供你参考
private DataTable CreaTable()
{
DataTable dt= new DataTable();
dt.Columns.Add("列1",typeof(string));
dt.Columns.Add("列2", typeof(int));
dt.Columns.Add("列3", typeof(string));
dt.Columns.Add("列4", typeof(string));
DataRow row, row1;
row = dt.NewRow();
row["列1"]= "行1";
row["列2"]= 1;
row["列3"]="d";
row["列4"]="a";
dt.Rows.Add(row);
row1= dt.NewRow();
row1["列1"] = "行2";
row1["列2"] = 12;
row1["列3"] = "b";
row1["列4"] = "c";
dt.Rows.Add(row1);
return dt;
}
private void button1_Click ( object sender , System.EventArgs e )
{
//==================transfer excel document to write data directly===================
// Excel.Application excel = new Excel.Application ( ) ;
// excel.Application.Workbooks.Add ( true ) ;
// excel.Cells[ 1 , 1 ] = "第一行第一列" ;
// excel.Cells[ 1 , 2 ] = "第一行第二列" ;
// excel.Cells[ 2 , 1 ] = "第二行第一列" ;
// excel.Cells[ 2 , 2 ] = "第二行第二列" ;
// excel.Cells[ 3 , 1 ] = "第三行第一列" ;
// excel.Cells[ 3 , 2 ] = "第三行第二列" ;
// excel.Visible = true ;

//=====================write data with DataTable=======================================
Excel.Application xlApp=new Excel.Application();
Excel.Workbook xlBook;
Excel.Worksheet xlSheet;
int rowIndex=1;
int colIndex=0;
object missing = Missing.Value;
// object fileName = "normal2.xls"; // template file name
// object newTemplate = false;



// Create a new Document, by calling the Add function in the Documents collection
xlBook= xlApp.Workbooks .Add (true);//.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
xlSheet=(Excel.Worksheet )xlApp.Worksheets.Add (missing,missing,missing,missing);

// xlBook =xlApp.Workbooks .Add ();
// xlSheet =xlApp.Worksheets.Add("sheet1");

DataTable Table=new DataTable();
Table = CreaTable();

//'将所得到的表的列名,赋值给单元格

// DataColumn Col;
// DataRow Row;
foreach(DataColumn Col in Table.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[1, colIndex] = Col.ColumnName;
}

//'得到的表所有行,赋值给单元格

foreach(DataRow Row in Table.Rows)
{
rowIndex = rowIndex + 1;

colIndex = 0;
foreach (DataColumn Col in Table.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = Row.ItemArray[colIndex-1];
}
}

xlSheet.get_Range(xlSheet.Cells[1,1], xlSheet.Cells[1, colIndex]).Font.Name = "黑体";
//'设标题为黑体字
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colIndex]).Font.Bold = true;
//'标题字体加粗
xlSheet.get_Range (xlSheet.Cells[1, 1], xlSheet.Cells[rowIndex, colIndex]).Borders.LineStyle = 1;
//'设表格边框样式


// xlSheet.PageSetup.LeftHeader = " &C ";
// xlSheet.PageSetup.CenterHeader = "&L";
// xlSheet.PageSetup.RightHeader ="&F";
// xlSheet.PageSetup.LeftFooter = "Speicherdatum: &Datum";
xlSheet.PageSetup.CenterFooter = "& &R page && of &N" ;
// xlSheet.PageSetup.RightFooter ="Dateiname: &F";

xlApp.Visible = true;
}
sjc0 2003-10-08
  • 打赏
  • 举报
回复
别人的看看对你有没有帮助?
只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了。
下面就让我们具体看看C#是如何使用这些东东吧。

1. 创建一个新Excel的Application:


Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started");
return 0;
}


2. 让这个工程可见:

exc.set_Visible(0, true);

3. 获取WorkBooks集合:

Workbooks workbooks = exc.Workbooks;

4. 加入新的WorkBook:

_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);

5. 获取WorkSheets集合:


_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR in worksheet == null");
}

6. 给单元格设置变量:


Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);


例程:


using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;

class Excel {
public static int Main() {
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started!");
return 0;
}

exc.set_Visible(0, true);
Workbooks workbooks = exc.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
Sheets sheets = workbook.Worksheets;

_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR: worksheet == null");
}

Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1);
return 100;
}
}

现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2;
const int nCell = 5;
Range range2 = worksheet.get_Range("A1", "E1");
int[] array2 = new int [nCell];
for (int i=0; i < array2.GetLength(0); i++) {
array2[i] = i+1;
}
Object[] args2 = new Object[1];
args2[0] = array2;
range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range2, args2);
树猫 2003-10-08
  • 打赏
  • 举报
回复
注解:DataSet在后台类定义成 public型,并实例化
<script language="javascript" type="text/javascript">
/**
* 名称 : createExcel
* 功能 : 生成记录结果集Excel文件
*/
function createExcel()
{
//保存标题名称
var arrTitle = new Array();
arrTitle[0] = "A";
arrTitle[1] = "B";
arrTitle[2] = "C";
arrTitle[3] = "D";
arrTitle[4] = "E";
arrTitle[5] = "F";
arrTitle[6] = "G";
arrTitle[7] = "H";
arrTitle[8] = "I";
arrTitle[9] = "J";
arrTitle[10] = "K";
arrTitle[11] = "L";
arrTitle[12] = "M";
arrTitle[13] = "N";
arrTitle[14] = "O";
arrTitle[15] = "P";
arrTitle[16] = "Q";
arrTitle[17] = "R";
arrTitle[18] = "S";
arrTitle[19] = "T";
arrTitle[20] = "U";
arrTitle[21] = "V";
arrTitle[22] = "W";
arrTitle[23] = "X";
arrTitle[24] = "Y";
arrTitle[25] = "Z";

//保存字段名称
var arrField = new Array();
var strField = new String();
strField = document.Excel.txtField.value;
arrField = strField.split(',');

//保存记录总数
var rowCount = parseInt('<%#dt.Rows.Count%>');

//保存记录结果集
var aRS = new Array();
//保存当前记录行数
var index = 0;

<%
for(int i=0; i<dt.Rows.Count; i++){
%>
aRS[index] = new Array();
var column = 0;
<%
for(int j=0; j<dt.Columns.Count; j++){
%>
aRS[index][column] = "<%=dt.Rows[i][j]%>";
column++;
<%
}
%>
index++;
<%
}
%>

//建立Excel应用程序oXL
var oXL = new ActiveXObject("Excel.Application");
//设置可见性
oXL.Visible = true;
//建立工作薄oWB
var oWB = oXL.Workbooks.Add();
//得到当前工作表
var oSheet = oWB.ActiveSheet;
//设置列的宽度
oSheet.Range("A1:Z1").ColumnWidth = 15.00;
//创建单元格对象
var rTitle = oSheet.Range("A1:Z1");
//水平中间对齐
rTitle.HorizontalAlignment = 3;

//显示标题信息
for(var i=0; i<arrField.length; i++)
{
var T = arrTitle[i];
oSheet.Range(T+"1").Value = arrField[i];
oSheet.Range(T+"1").BorderAround(LineStyle=1);
}

oXL.StatusBar = "现在正在生成,请等待...";

//显示记录信息
for(var i=0; i<aRS.length; i++)
{
for(var j=0; j<arrField.length; j++)
{
var T = arrTitle[j];
oSheet.Range(T+(i+2)).Value = "'" + aRS[i][j].toString();
oSheet.Range(T+(i+2)).BorderAround(LineStyle=1);
}
}

oXL.StatusBar = "报表已经生成完毕.";
}
</script>
kdi2003 2003-10-08
  • 打赏
  • 举报
回复
关注这个问题。
to:freecs
客户端没有Excel怎么办?

to:BirdInParadise
服务器端的Excel会引起安全问题否?

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧