请问如何把dataset里面的表导出到excel中?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请问如何把dataset里面的表导出到excel中?相关的知识,希望对你有一定的参考价值。

谢谢!最好全部倒入。

将DataSet中的DataTable作为参数传入下面的方法,再将你要保存的完整路径名作为第二个参数传入下面的方法,就可以将数据导入到Excel中
public void CreateExcel(DataTable dt,string fileName)

System.Diagnostics.Process[] arrProcesses;
arrProcesses = System.Diagnostics.Process.GetProcessesByName("Excel");
foreach (System.Diagnostics.Process myProcess in arrProcesses)

myProcess.Kill();


Object missing = Missing.Value;
Microsoft.Office.Interop.Excel.Application m_objExcel =

new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks m_objWorkBooks = m_objExcel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook m_objWorkBook = m_objWorkBooks.Add(true);
Microsoft.Office.Interop.Excel.Sheets m_objWorkSheets = m_objWorkBook.Sheets; ;
Microsoft.Office.Interop.Excel.Worksheet m_objWorkSheet =

(Microsoft.Office.Interop.Excel.Worksheet)m_objWorkSheets[1];
int intFeildCount = dt.Columns.Count;
for (int col = 0; col < intFeildCount; col++)

m_objWorkSheet.Cells[1, col + 1] = dt.Columns[col].ToString();

for (int intRowCount = 0; intRowCount < dt.Rows.Count; intRowCount++)

for (int intCol = 0; intCol < dt.Columns.Count; intCol++)

m_objWorkSheet.Cells[intRowCount + 2, intCol + 1] = "'"+dt.Rows[intRowCount][intCol].ToString();



if (File.Exists(fileName))

File.Delete(fileName);

m_objWorkBook.SaveAs(fileName, missing, missing, missing, missing,

missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);

m_objExcel = null;

参考技术A 以前用到了,再网上找的

/// <summary>
/// 把DataGridView到导出到Excel中
/// </summary>
/// <param name="gridView">数据源DataGridView</param>
/// <param name="fileName">保存文件名称</param>
/// <param name="isShowExcle">是否显示Excel界面</param>
/// <param name="exceptionInfo">返回异常信息</param>
/// <returns>导出是否成功</returns>
private bool ExportForDataGridview(System.Windows.Forms.DataGridView gridView, string fileName, bool isShowExcle, out string exceptionInfo)

bool isok = false;
exceptionInfo = "";
Microsoft.Office.Interop.Excel.Application app = null;
try

//建立Excel对象
app = new Microsoft.Office.Interop.Excel.Application();

catch

exceptionInfo = "无法创建Excel对象,请确保您的机器安装有MicroSoft Office 2003。";
isok = false;

if (app != null)

try

app.Visible = isShowExcle;
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null)

exceptionInfo = "无法创建Excel工作表,未知错误。";
isok = false;

else

string sLen = "";
//取得最后一列列名
char H = (char)(64 + gridView.ColumnCount / 26);
char L = (char)(64 + gridView.ColumnCount % 26);
if (gridView.ColumnCount < 26)

sLen = L.ToString();

else

sLen = H.ToString() + L.ToString();


//标题
string sTmp = sLen + "1";
Range ranCaption = worksheet.get_Range(sTmp, "A1");
string[] asCaption = new string[gridView.ColumnCount];
for (int i = 0; i < gridView.ColumnCount; i++)

asCaption[i] = gridView.Columns[i].HeaderText;

ranCaption.Value2 = asCaption;

//数据
object[] obj = new object[gridView.Columns.Count];
for (int r = 0; r < gridView.RowCount - 1; r++)

for (int l = 0; l < gridView.Columns.Count; l++)

if (gridView[l, r].ValueType == typeof(DateTime))

obj[l] = gridView[l, r].Value.ToString();

else if (gridView[l, r].ValueType == typeof(String))

obj[l] = "'" + gridView[l, r].Value.ToString();

else

obj[l] = gridView[l, r].Value;


string cell1 = sLen + ((int)(r + 2)).ToString();
string cell2 = "A" + ((int)(r + 2)).ToString();
Range ran = worksheet.get_Range(cell1, cell2);
ran.Value2 = obj;


//保存
workbook.SaveCopyAs(fileName);
workbook.Saved = true;
isok = true;


catch (Exception err)

exceptionInfo = err.Message;
isok = false;

finally

if (app != null)

//关闭
app.UserControl = false;
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;



return isok;

参考技术B 很想回答你的问题,但是看到你是五级经理,提出问题竟然不给分,太不尊重别人了吧!!!

你得到了你还不懂的知识,别人得到了虚荣心的满足,双赢啊!

如何把PowerDesigner 8的表设计导到EXCEL中

参考技术A PowerDesigner 8
是什么?
PowerPCB?

以上是关于请问如何把dataset里面的表导出到excel中?的主要内容,如果未能解决你的问题,请参考以下文章

请问DISCUZ里面怎么把注册的用户数据全部导出excel表格。

java如何将查询到的表中数据导出到excel中(包含字段名)

Excel 请问如何用Web查询得到网页上的所有数据

怎样把access里面的上百万的数据导出到excel里面来?我现在是2010office。

ASP.NET把数据查询出来保存在了一个datatable里面了,之后我想把这个datatable的数据导入到access数据库

写了一个把dataset导出到Excel的winform程序,点击导出就报错,下面是错误信息,谁帮我看看怎么回事?