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

谢谢!最好全部倒入。... 谢谢!最好全部倒入。 展开
 我来答
hsnhhsnh
推荐于2016-10-22
知道答主
回答量:14
采纳率:0%
帮助的人:0
展开全部
将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;

}
百度网友6eed2f8
2008-11-25 · TA获得超过5082个赞
知道大有可为答主
回答量:3423
采纳率:0%
帮助的人:3819万
展开全部
以前用到了,再网上找的

/// <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;

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wwdjs
2008-11-25 · TA获得超过7493个赞
知道大有可为答主
回答量:2855
采纳率:50%
帮助的人:2372万
展开全部
很想回答你的问题,但是看到你是五级经理,提出问题竟然不给分,太不尊重别人了吧!!!

你得到了你还不懂的知识,别人得到了虚荣心的满足,双赢啊!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式