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

谢谢!最好全部倒入。
2024-12-01 10:40:05
推荐回答(3个)
回答1:

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

}

回答2:

以前用到了,再网上找的

///


/拿哪// 把DataGridView到导出到Excel中
///

/// 数据源DataGridView
/// 保存文件名称
/// 是否显示Excel界面
/// 返回异常信息
/// 导出是否成功
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;

}

回答3:

很想回答你的问题,但是看到你是五级经理,提出凳渗问题枣悉脊竟然不给分,太不尊重别人了吧!!!

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