VS2005 如何写入Excel RT,各位大虾们最好能贴上代码,方便理解! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 google一下吧,很多vc操作excel教程的. http://www.vckbase.com/document/viewdoc/?id=1168 // OFFICE-EXCEL 从DataTable导出EXCEL文件// winform 从DataTable导出EXCEL文件public void ExportExcelOffice(DataTable table, string filepath){ Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Visible = false; object ms = Type.Missing; Excel.Workbook wk = excel.Workbooks.Add(ms); Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet; for (int i = 0; i < table.Columns.Count; i++) { ws.Cells[1, i + 1] = table.Columns[i].ColumnName; } for (int i = 0; i < table.Rows.Count; i++) { for (int j = 0; j < table.Columns.Count; j++) { ws.Cells[i + 2, j + 1] = table.Rows[i][j].ToString(); } } if (File.Exists(filepath) == false) { Directory.CreateDirectory(filepath); } wk.SaveAs(filepath, ms, ms, ms, ms, ms, Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms); excel.Quit();}// webform 从DataTable导出EXCEL文件public void ExportExcelStream(DataTable table, string filepath){ StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); DataGrid excel = new DataGrid(); System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle(); System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle(); System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle(); AlternatingStyle.BackColor = System.Drawing.Color.LightGray; headerStyle.BackColor = System.Drawing.Color.LightGray; headerStyle.Font.Bold = true; headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; ; excel.AlternatingItemStyle.MergeWith(AlternatingStyle); excel.HeaderStyle.MergeWith(headerStyle); excel.ItemStyle.MergeWith(itemStyle); excel.GridLines = GridLines.Both; excel.HeaderStyle.Font.Bold = true; excel.DataSource = table.DefaultView; //输出DataTable的内容 excel.DataBind(); excel.RenderControl(htmlWriter); string filestr = filepath; int pos = filestr.LastIndexOf("\\"); string file = filestr.Substring(0, pos); if (!Directory.Exists(file)) { Directory.CreateDirectory(file); } System.IO.StreamWriter sw = new StreamWriter(filestr); sw.Write(stringWriter.ToString()); sw.Close();} 调试相关的一二事 非框架视CFormView中,无法获取控件 [冒死发帖]dll源代码调试全攻略~包括ISAPI,API HOOK,GINA等dll的源代码调试方法~ 屏蔽文件中的光标和“另存为” 如何获取“显示桌面”事件 问一下,socket通信怎么发送struct类型的数据,以及怎么接受啊? 请问clistctrl中怎样限制只能选择第2列? 如何改变系统字体 状态栏上添加按钮。(没银子了,以后补) 请问“宏”是什么?和宏病毒有关系吗?(来着有分) DLL(C++)如何操作VB中WebBrowser对象? 如何清除掉socket发送缓存区中的数据
// OFFICE-EXCEL 从DataTable导出EXCEL文件// winform 从DataTable导出EXCEL文件
public void ExportExcelOffice(DataTable table, string filepath)
{ Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
object ms = Type.Missing;
Excel.Workbook wk = excel.Workbooks.Add(ms);
Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
for (int i = 0; i < table.Columns.Count; i++)
{
ws.Cells[1, i + 1] = table.Columns[i].ColumnName;
}
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = table.Rows[i][j].ToString();
}
} if (File.Exists(filepath) == false)
{
Directory.CreateDirectory(filepath);
}
wk.SaveAs(filepath, ms, ms, ms, ms, ms, Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms);
excel.Quit();}// webform 从DataTable导出EXCEL文件
public void ExportExcelStream(DataTable table, string filepath)
{
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
DataGrid excel = new DataGrid();
System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();
System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();
System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();
AlternatingStyle.BackColor = System.Drawing.Color.LightGray;
headerStyle.BackColor = System.Drawing.Color.LightGray;
headerStyle.Font.Bold = true;
headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; ; excel.AlternatingItemStyle.MergeWith(AlternatingStyle);
excel.HeaderStyle.MergeWith(headerStyle);
excel.ItemStyle.MergeWith(itemStyle);
excel.GridLines = GridLines.Both;
excel.HeaderStyle.Font.Bold = true;
excel.DataSource = table.DefaultView; //输出DataTable的内容
excel.DataBind();
excel.RenderControl(htmlWriter); string filestr = filepath;
int pos = filestr.LastIndexOf("\\");
string file = filestr.Substring(0, pos);
if (!Directory.Exists(file))
{
Directory.CreateDirectory(file);
}
System.IO.StreamWriter sw = new StreamWriter(filestr);
sw.Write(stringWriter.ToString());
sw.Close();
}