asp.net(c#)如何将从数据库获得的数据添加到excel
具体要求是:excel表已经存在,并且里面已经有一些数据,如何能将从数据库获得的一条或多条数据记录添加到已有的数据后面?(最好有程序或方法之类的代码,不要笼统的说用什么什...
具体要求是:excel表已经存在,并且里面已经有一些数据,如何能将从数据库获得的一条或多条数据记录添加到已有的数据后面?(最好有程序或方法之类的代码,不要笼统的说用什么什么方法...)
好不容易把这段代码融进了我的程序,可是它不能达到我要的效果啊,每次执行它都会把原来表中的数据覆盖,而不能添加到后面 展开
好不容易把这段代码融进了我的程序,可是它不能达到我要的效果啊,每次执行它都会把原来表中的数据覆盖,而不能添加到后面 展开
展开全部
因为excel和C#都是微软自家的东东,C#中就有专门的方法导入导出office套件的格式:
这是一个别人写的例子,就用的C#中自带的office处理方法,你看看吧,特别方便
方法如下:
using System;
using System.Data;
using System.Diagnostics;
using System.Reflection;
namespace RC.MSOffice
{
/// <summary>
/// Excel相关
/// </summary>
public class ExcelUtility
{
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dataSet">需要导出Excel的DataSet</param>
/// <param name="fileName">导出的路径</param>
public static void ExportToExcel(DataSet dataSet, string fileName)
{
if (dataSet.Tables.Count == 0)
{
throw new Exception("DataSet中没有任何可导出的表。");
}
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
excelApplication.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Workbook workbook = excelApplication.Workbooks.Add(Missing.Value);
foreach (DataTable dt in dataSet.Tables)
{
Microsoft.Office.Interop.Excel.Worksheet lastWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(workbook.Worksheets.Count);
Microsoft.Office.Interop.Excel.Worksheet newSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, lastWorksheet, Type.Missing, Type.Missing);
newSheet.Name = dt.TableName;
for (int col = 0; col < dt.Columns.Count; col++)
{
newSheet.Cells[1, col + 1] = dt.Columns[col].ColumnName;
}
for (int row = 0; row < dt.Rows.Count; row++)
{
for (int col = 0; col < dt.Columns.Count; col++)
{
newSheet.Cells[row + 2, col + 1] = dt.Rows[row][col].ToString();
}
}
}
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Activate();
try
{
workbook.Close(true, fileName, System.Reflection.Missing.Value);
}
catch (Exception e)
{
throw e;
}
excelApplication.Quit();
KillExcel();
}
private static void KillExcel()
{
Process[] excelProcesses = Process.GetProcessesByName("EXCEL");
DateTime startTime = new DateTime();
int processId = 0;
for (int i = 0; i < excelProcesses.Length; i++)
{
if (startTime < excelProcesses[i].StartTime)
{
startTime = excelProcesses[i].StartTime;
processId = i;
}
}
if (excelProcesses[processId].HasExited == false)
{
excelProcesses[processId].Kill();
}
}
}
}
这是一个别人写的例子,就用的C#中自带的office处理方法,你看看吧,特别方便
方法如下:
using System;
using System.Data;
using System.Diagnostics;
using System.Reflection;
namespace RC.MSOffice
{
/// <summary>
/// Excel相关
/// </summary>
public class ExcelUtility
{
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dataSet">需要导出Excel的DataSet</param>
/// <param name="fileName">导出的路径</param>
public static void ExportToExcel(DataSet dataSet, string fileName)
{
if (dataSet.Tables.Count == 0)
{
throw new Exception("DataSet中没有任何可导出的表。");
}
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
excelApplication.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Workbook workbook = excelApplication.Workbooks.Add(Missing.Value);
foreach (DataTable dt in dataSet.Tables)
{
Microsoft.Office.Interop.Excel.Worksheet lastWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(workbook.Worksheets.Count);
Microsoft.Office.Interop.Excel.Worksheet newSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing, lastWorksheet, Type.Missing, Type.Missing);
newSheet.Name = dt.TableName;
for (int col = 0; col < dt.Columns.Count; col++)
{
newSheet.Cells[1, col + 1] = dt.Columns[col].ColumnName;
}
for (int row = 0; row < dt.Rows.Count; row++)
{
for (int col = 0; col < dt.Columns.Count; col++)
{
newSheet.Cells[row + 2, col + 1] = dt.Rows[row][col].ToString();
}
}
}
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Delete();
((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1)).Activate();
try
{
workbook.Close(true, fileName, System.Reflection.Missing.Value);
}
catch (Exception e)
{
throw e;
}
excelApplication.Quit();
KillExcel();
}
private static void KillExcel()
{
Process[] excelProcesses = Process.GetProcessesByName("EXCEL");
DateTime startTime = new DateTime();
int processId = 0;
for (int i = 0; i < excelProcesses.Length; i++)
{
if (startTime < excelProcesses[i].StartTime)
{
startTime = excelProcesses[i].StartTime;
processId = i;
}
}
if (excelProcesses[processId].HasExited == false)
{
excelProcesses[processId].Kill();
}
}
}
}
展开全部
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory + "(已有的EXCEL).xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
DataView dv = (DataView)gridView1.DataSource;
System.Data.DataTable dt = dv.ToTable();
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
excel.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
}
double num = ((double)i / (dt.Rows.Count-1))*100;
lblExporting.Text = "正在导出,请等待...已导出" + num + "%";
}
excel.Application.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory + "(已有的EXCEL).xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
DataView dv = (DataView)gridView1.DataSource;
System.Data.DataTable dt = dv.ToTable();
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
excel.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
}
double num = ((double)i / (dt.Rows.Count-1))*100;
lblExporting.Text = "正在导出,请等待...已导出" + num + "%";
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询