如何将datagridview控件上的数据引到excel...请高手指点一下.``
小弟没分了..请高手指教一下.``
小弟没分了..请高手指教一下.``
解决方案 »
- 端口异步扫描问题
- RSA非对称加密解密的问题,不能获得明文!!!!
- 80端口转换
- (服务器控件)页面中有5个textbox框,分别为txt1,txt2 ……txt5 有没有办法通过循环分别给他们赋不同的值(变量)
- 如何響應多個button_click
- 急!!3个域用户管理的web系统,不能修改,怎样实现单点登录SSO,今天揭贴
- 一個很弱智的C#應用程序問題
- 统计一个文本中有多少个英文单词 如何用c# 或js来实现
- asp服务器控件也能像普通HTML控件一样调用javascript方法?
- StreamReader StreamWriter 怎么读写中文
- 能不能让一个hyperlink看起来像一个button?
- 如何获取即时的股价信息
数据导入Execl吗?还是引到,什么是引到呢?
我有源码。
public void WriteToExcel(DataTable table)//查询结果导出Execl
{
try
{
string tempImagePath = Application.StartupPath;//取你程序安装的目录
string temp = tempImagePath + "\\ExeclFiles";
Directory.CreateDirectory(@temp);
string strFilePath = @Application.StartupPath + "\\ExeclFiles\\" + LitianClass.GetServerTimeStr() + ".xls";
System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, true, System.Text.Encoding.Default);
object[] values = new object[table.Columns.Count];
sw.Write("\r\n");
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < values.Length; ++j)
{
sw.Write(table.Rows[i][j].ToString());
sw.Write('\t');
}
sw.Write("\r\n");
}
sw.Flush();
sw.Close();
LitianClass.MessageBoxOK("成功导出[" + ds.Tables[0].Rows.Count.ToString() + "]行到Execl!");
}
catch
{
LitianClass.MessageBoxNo("导出Execl失败!");
}
}
#endregion
...{
System.Data.DataTable myTable = (System.Data.DataTable)grid.DataSource; try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
int rowIndex;
int colIndex;
rowIndex = 2;
colIndex = 0;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
if (grid.Rows.Count > 0)
{
Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, grid.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true; foreach (DataGridViewColumn colu in grid.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = colu.HeaderText;
} for (int row = 0; row < myTable.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < grid.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = grid.Rows[row].Cells[col].Value;
}
}
}
else
{
Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, myTable.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true;
foreach (DataColumn Col in myTable.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = Col.ColumnName;
}
for (int row = 0; row < myTable.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < myTable.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = grid.Rows[row].Cells[col].Value;
}
}
}
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Bold = true;
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowIndex, colIndex]).Borders.LineStyle = 1; xlApp.Cells.EntireColumn.AutoFit();
xlApp.Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
xlApp.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
xlApp.Visible = true;
}
catch (Exception e)
{
throw e;
}
}
http://topic.csdn.net/u/20081203/09/6bc987dc-7d65-466e-a2de-aefe02c6cbc5.html