在asp.net中如何将 SQL2000的数据导出到Excel2010中...
以前都是Excel2003的, 现在是Excel2010的...
请高人指点...
11 个解决方案
#1
导出方法一样,可兼容
#2
public void InsertIntoExcel(Context.MainContext context)
{
Microsoft.Office.Interop.Excel.Application excelApp = new ApplicationClass();
object missingValue = Type.Missing;
Workbook workBook = excelApp.Workbooks.Open(Directory.GetCurrentDirectory() + Program.SearchSingleMessage("M2"),
missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue,
missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
// 试验扰动的数据时,活动的表单为第2个;记录正常数据时,活动的表单改为第1个
Worksheet workSheet;
if (context.underDisturbance)
{
workSheet = (Worksheet)workBook.Sheets[2];
}
else
{
workSheet = (Worksheet)workBook.Sheets[1];
}
workSheet.Activate();
if (context.type == Program.ObjectType.DieselEngine)
{
for (int i = 0; i < Program.w.Length; i++)
{
workSheet.Cells[1 + i, 1] = Program.t[i].ToString();
workSheet.Cells[1 + i, 2] = Program.w[i].ToString();
workSheet.Cells[1 + i, 3] = Program.Td[i].ToString();
}
}
else if (context.type == Program.ObjectType.PowerStation)
{
for (int i = 0; i < Program.w.Length; i++)
{
workSheet.Cells[1 + i, 1] = Program.t[i].ToString();
workSheet.Cells[1 + i, 2] = Program.w[i].ToString();
workSheet.Cells[1 + i, 3] = Program.U[i].ToString();
workSheet.Cells[1 + i, 4] = Program.delta[i].ToString();
workSheet.Cells[1 + i, 5] = Program.Psai[i].ToString();
workSheet.Cells[1 + i, 6] = Program.I[i].ToString();
workSheet.Cells[1 + i, 7] = Program.Ia[i].ToString();
workSheet.Cells[1 + i, 8] = Program.Ib[i].ToString();
workSheet.Cells[1 + i, 9] = Program.Ic[i].ToString();
}
}
else if (context.type == Program.ObjectType.CUEP)
{
workSheet.Cells[1, 1] = "t";
workSheet.Cells[1, 2] = "w";
workSheet.Cells[1, 3] = "U";
workSheet.Cells[1, 4] = "delta";
workSheet.Cells[1, 5] = "Psai";
workSheet.Cells[1, 6] = "I";
workSheet.Cells[1, 7] = "Ia";
workSheet.Cells[1, 8] = "Ib";
workSheet.Cells[1, 9] = "Ic";
workSheet.Cells[1, 20] = "E'q";
workSheet.Cells[1, 21] = "Ud";
workSheet.Cells[1, 22] = "Uq";
for (int i = 0; i < Program.w.Length; i++)
{
try
{
workSheet.Cells[2 + i, 1] = Program.t[i].ToString();
workSheet.Cells[2 + i, 2] = Program.w[i].ToString();
workSheet.Cells[2 + i, 3] = Program.U[i].ToString();
workSheet.Cells[2 + i, 4] = Program.delta[i].ToString();
workSheet.Cells[2 + i, 5] = Program.Psai[i].ToString();
workSheet.Cells[2 + i, 6] = Program.I[i].ToString();
workSheet.Cells[2 + i, 7] = Program.Ia[i].ToString();
workSheet.Cells[2 + i, 8] = Program.Ib[i].ToString();
workSheet.Cells[2 + i, 9] = Program.Ic[i].ToString();
// 相关因子的字符形式与相关因子的模
workSheet.Cells[2 + i, 10] = ((Complex)((Complex[])Program.relatedFacotr[i])[0]).ToString();
workSheet.Cells[2 + i, 11] = ((Complex)((Complex[])Program.relatedFacotr[i])[0]).Abs().ToString();
workSheet.Cells[2 + i, 12] = ((Complex)((Complex[])Program.relatedFacotr[i])[1]).ToString();
workSheet.Cells[2 + i, 13] = ((Complex)((Complex[])Program.relatedFacotr[i])[1]).Abs().ToString();
workSheet.Cells[2 + i, 14] = ((Complex)((Complex[])Program.relatedFacotr[i])[2]).ToString();
workSheet.Cells[2 + i, 15] = ((Complex)((Complex[])Program.relatedFacotr[i])[2]).Abs().ToString();
workSheet.Cells[2 + i, 16] = ((Complex)((Complex[])Program.relatedFacotr[i])[3]).ToString();
workSheet.Cells[2 + i, 17] = ((Complex)((Complex[])Program.relatedFacotr[i])[3]).Abs().ToString();
workSheet.Cells[2 + i, 18] = ((Complex)((Complex[])Program.relatedFacotr[i])[4]).ToString();
workSheet.Cells[2 + i, 19] = ((Complex)((Complex[])Program.relatedFacotr[i])[4]).Abs().ToString();
// 除了转速与功角的状态变量记录下来
workSheet.Cells[2 + i, 20] = Program.E1q[i].ToString();
workSheet.Cells[2 + i, 21] = Program.Ud[i].ToString();
workSheet.Cells[2 + i, 22] = Program.Uq[i].ToString();
}
catch (Exception ex)
{
break;
}
}
}
workBook.Save();
workBook.Close(true, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
workSheet = null;
workBook = null;
excelApp = null;
GC.Collect();
}
#4
打开excel二维数组赋值
gridview导出
xml形式导出
gridview导出
xml形式导出
#5
兼容2003,2010格式的导出方法
http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx
http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx
#7
我去试试先...
#8
我原先的代码在调试时候,就可以正常导出,不调试运行,就不能正常导出
错误提示是: Microsoft Jet 数据库引擎打不开文件...
应该是权限的问题吧...
请高手指点...
错误提示是: Microsoft Jet 数据库引擎打不开文件...
应该是权限的问题吧...
请高手指点...
#9
在文件夹给net用户写入的权限。
#10
NPOI
#11
#1
导出方法一样,可兼容
#2
public void InsertIntoExcel(Context.MainContext context)
{
Microsoft.Office.Interop.Excel.Application excelApp = new ApplicationClass();
object missingValue = Type.Missing;
Workbook workBook = excelApp.Workbooks.Open(Directory.GetCurrentDirectory() + Program.SearchSingleMessage("M2"),
missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue,
missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);
// 试验扰动的数据时,活动的表单为第2个;记录正常数据时,活动的表单改为第1个
Worksheet workSheet;
if (context.underDisturbance)
{
workSheet = (Worksheet)workBook.Sheets[2];
}
else
{
workSheet = (Worksheet)workBook.Sheets[1];
}
workSheet.Activate();
if (context.type == Program.ObjectType.DieselEngine)
{
for (int i = 0; i < Program.w.Length; i++)
{
workSheet.Cells[1 + i, 1] = Program.t[i].ToString();
workSheet.Cells[1 + i, 2] = Program.w[i].ToString();
workSheet.Cells[1 + i, 3] = Program.Td[i].ToString();
}
}
else if (context.type == Program.ObjectType.PowerStation)
{
for (int i = 0; i < Program.w.Length; i++)
{
workSheet.Cells[1 + i, 1] = Program.t[i].ToString();
workSheet.Cells[1 + i, 2] = Program.w[i].ToString();
workSheet.Cells[1 + i, 3] = Program.U[i].ToString();
workSheet.Cells[1 + i, 4] = Program.delta[i].ToString();
workSheet.Cells[1 + i, 5] = Program.Psai[i].ToString();
workSheet.Cells[1 + i, 6] = Program.I[i].ToString();
workSheet.Cells[1 + i, 7] = Program.Ia[i].ToString();
workSheet.Cells[1 + i, 8] = Program.Ib[i].ToString();
workSheet.Cells[1 + i, 9] = Program.Ic[i].ToString();
}
}
else if (context.type == Program.ObjectType.CUEP)
{
workSheet.Cells[1, 1] = "t";
workSheet.Cells[1, 2] = "w";
workSheet.Cells[1, 3] = "U";
workSheet.Cells[1, 4] = "delta";
workSheet.Cells[1, 5] = "Psai";
workSheet.Cells[1, 6] = "I";
workSheet.Cells[1, 7] = "Ia";
workSheet.Cells[1, 8] = "Ib";
workSheet.Cells[1, 9] = "Ic";
workSheet.Cells[1, 20] = "E'q";
workSheet.Cells[1, 21] = "Ud";
workSheet.Cells[1, 22] = "Uq";
for (int i = 0; i < Program.w.Length; i++)
{
try
{
workSheet.Cells[2 + i, 1] = Program.t[i].ToString();
workSheet.Cells[2 + i, 2] = Program.w[i].ToString();
workSheet.Cells[2 + i, 3] = Program.U[i].ToString();
workSheet.Cells[2 + i, 4] = Program.delta[i].ToString();
workSheet.Cells[2 + i, 5] = Program.Psai[i].ToString();
workSheet.Cells[2 + i, 6] = Program.I[i].ToString();
workSheet.Cells[2 + i, 7] = Program.Ia[i].ToString();
workSheet.Cells[2 + i, 8] = Program.Ib[i].ToString();
workSheet.Cells[2 + i, 9] = Program.Ic[i].ToString();
// 相关因子的字符形式与相关因子的模
workSheet.Cells[2 + i, 10] = ((Complex)((Complex[])Program.relatedFacotr[i])[0]).ToString();
workSheet.Cells[2 + i, 11] = ((Complex)((Complex[])Program.relatedFacotr[i])[0]).Abs().ToString();
workSheet.Cells[2 + i, 12] = ((Complex)((Complex[])Program.relatedFacotr[i])[1]).ToString();
workSheet.Cells[2 + i, 13] = ((Complex)((Complex[])Program.relatedFacotr[i])[1]).Abs().ToString();
workSheet.Cells[2 + i, 14] = ((Complex)((Complex[])Program.relatedFacotr[i])[2]).ToString();
workSheet.Cells[2 + i, 15] = ((Complex)((Complex[])Program.relatedFacotr[i])[2]).Abs().ToString();
workSheet.Cells[2 + i, 16] = ((Complex)((Complex[])Program.relatedFacotr[i])[3]).ToString();
workSheet.Cells[2 + i, 17] = ((Complex)((Complex[])Program.relatedFacotr[i])[3]).Abs().ToString();
workSheet.Cells[2 + i, 18] = ((Complex)((Complex[])Program.relatedFacotr[i])[4]).ToString();
workSheet.Cells[2 + i, 19] = ((Complex)((Complex[])Program.relatedFacotr[i])[4]).Abs().ToString();
// 除了转速与功角的状态变量记录下来
workSheet.Cells[2 + i, 20] = Program.E1q[i].ToString();
workSheet.Cells[2 + i, 21] = Program.Ud[i].ToString();
workSheet.Cells[2 + i, 22] = Program.Uq[i].ToString();
}
catch (Exception ex)
{
break;
}
}
}
workBook.Save();
workBook.Close(true, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
workSheet = null;
workBook = null;
excelApp = null;
GC.Collect();
}
#3
#4
打开excel二维数组赋值
gridview导出
xml形式导出
gridview导出
xml形式导出
#5
兼容2003,2010格式的导出方法
http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx
http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx
#6
#7
我去试试先...
#8
我原先的代码在调试时候,就可以正常导出,不调试运行,就不能正常导出
错误提示是: Microsoft Jet 数据库引擎打不开文件...
应该是权限的问题吧...
请高手指点...
错误提示是: Microsoft Jet 数据库引擎打不开文件...
应该是权限的问题吧...
请高手指点...
#9
在文件夹给net用户写入的权限。
#10
NPOI