日期:2014-05-19  浏览次数:20862 次

将一张datatable中的值导到excel中,怎么弄?
excel   11.我一条条导入,速度实在是不行..有没有好点的方法?

------解决方案--------------------
看看这里,应该可以帮到你
http://blog.csdn.net/simonllf/archive/2006/12/13/1441672.aspx
------解决方案--------------------
创建一个文本文件不过 后坠名为 xls
直接写成html Table 到里面
这样最快,而且不用任何组建


或用 ODBC 写。
------解决方案--------------------
我在项目中用的导出excel的代码:
Response.Buffer= true;
string sExcelFileName= "Items.xls ";
Response.AppendHeader( "Content-Disposition ", "attachment;filename= "+sExcelFileName);
Response.ContentType = "application/ms-excel ";
Response.Charset= "gb3212 ";
Response.ContentEncoding=System.Text.Encoding.Default;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo( "ZH-CN ",true);
System.IO.StringWriter tw = new System.IO.StringWriter(myCItrad) ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
this.DataGrid1.AllowPaging=false;
BindGrid();
this.DataGrid1.RenderControl(hw);//输出
Response.Write(tw.ToString());
tw.Close();
Response.End();
------解决方案--------------------
我想你可以把datagrid放到table里面,table中的格式你想怎么加都可以,从table输出就因该可以了,你试试看。
------解决方案--------------------
using System;
using System.IO;
using Excel;

namespace Openet.Utility.ExcelTools
{
/// <summary>
/// ExcelOption 的摘要说明。
/// </summary>
public class ExcelTools
{
public ExcelTools()
{

}

/// <summary>
/// 导出到EXCEL表
/// </summary>
public static void toExcel(string FileName, string SheetName, System.Data.DataTable dt)
{
Excel.ApplicationClass myExcel = new Excel.ApplicationClass();
Excel.Worksheet worksheet = null;

string curPath = Directory.GetParent(FileName).FullName;
if(!Directory.Exists(curPath))
{
Directory.CreateDirectory(curPath);
}

if (File.Exists(FileName))
{
try
{
File.Delete(FileName);
}
catch
{
throw new Exception( "文件操作出错,可能正在运行当前的EXCEL文件! ");
}
}

try
{
object missing = System.Reflection.Missing.Value;

myExcel.Workbooks.Add(missing);
worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
worksheet.Name = SheetName;
myExcel.Visible = false;


int iRows = dt.Rows.Count;
int iCol = dt.Columns.Count;

worksheet.get_Range( worksheet.Cells[1, 1], worksheet.Cells[ iRows+1, iCol] ).NumberFormatLocal = "@ ";

for( int i=0; i <iCol; i++)
{
worksheet.Cells[1,i+1] = dt.Columns[i].ToString();
}

int size = iRows / 1000;
int page = 0;
string[,] rows = null;
for(int i=0; i <iRows; )
{
if(page <size)
{
rows = new string[1000,iCol];
for(int count=0;count <1000;count++)
{
for(int j=0; j <iCol; j++)
{
rows[count,j] = dt.DefaultView[i][j].ToString();
}
i++;
}
worksheet.get_Range(worksheet.Cells[page * 1000 + 2, 1], worksheet.Cells[ page * 1000 + 1001, iCol]).Value2 = rows;
page++;
}
else