datagrideview with Excel
2009-10-20 12:48:41| 分类:
IT
| 标签:
|举报
|字号大中小 订阅
【lsj_zrp】:
/// <summary>
/// 以操作Excel控件的方式将DataGridView数据导出到Excel add by sunny 2007/1/18
/// </summary>
/// <param name="GridView">DataGridView对象</param>
/// <param name="strExcelFile">Excel文件名</param>
/// <param name="strError">out参数,返回出错信息</param>
/// <returns>
/// -1 出错
/// 0 成功
/// </returns>
public static int DataGridViewToExcel(System.Windows.Forms.DataGridView GridView,
string strExcelFileName,
out string strError)
{
strError = "";
int nRet = 0;
Excel.Application xlApp = new Excel.Application();
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
try
{
//~~
// 写字段名
for (int i = 0; i < GridView.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = GridView.Columns[i].HeaderText.ToString();
}
// 写记录
for (int i = 0; i < GridView.Rows.Count; i++)
{
for (int j = 0; j < GridView.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = GridView.Rows[i].Cells[j].Value.ToString(); ;
}
}
worksheet.Columns.EntireColumn.AutoFit();//自动适应每列的宽度 add by sunny.li
Excel.Range rg = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, GridView.Columns.Count]);
rg.Font.Bold = true;
workbook.Saved = true;
workbook.SaveCopyAs(strExcelFileName);
// 关掉内存中的进程
xlApp.Quit();
nRet = 0;
}
catch (Exception ex)
{
strError = ex.ToString();
nRet = -1;
}
return nRet;
}
【lsj_zrp】:
Excel内容读入到datagridview中
public const string EXCELCONNECTION = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ " Extended Properties='Excel 8.0;"
+ " HDR=YES';";
public const string EXCEL_FILTER = "Microsoft Office Excel 工作簿 (﹡.xls)|*.xls";
#endregion
/// <summary>
/// 将Excel文件的数据导出到DataTable
/// </summary>
/// <param name="strExcelFileName">Excel 文件名</param>
/// <param name="dt">out参数,返回DataTable</param>
/// <param name="strError">out参数,返回出错信息</param>
/// <returns>
/// -1 出错
/// 0 成功
/// </returns>
/// 注:本函数目前只导出Sheet1$表的数据
public static int Excel2DataTable(string strExcelFileName,
out DataTable dt,
out string strError)
{
strError = "";
int nRet = 0;
dt = null;
if (String.IsNullOrEmpty(strExcelFileName) == true)
{
strError = "strExcelFile参数不能为空";
return -1;
}
if (File.Exists(strExcelFileName) == false)
{
strError = "文件'" + strExcelFileName + "'不存在";
return -1;
}
// 执行导出数据
string strConnection = ExcelADOUtil.EXCELCONNECTION
+ "Data Source=" + strExcelFileName + ";";
OleDbConnection connection = new OleDbConnection(strConnection);
connection.Open();
try
{
//-----------------------------------
string strCommand = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter adapter = new OleDbDataAdapter(strCommand, connection);
dt = new DataTable();
int nCount = adapter.Fill(dt);
//-------------------------------------
}
catch (Exception ex)
{
strError = ex.Message;
return -1;
}
finally
{
connection.Close();
}
return 0;
}
【lsj_zrp】:
把datagridview的数据导入的文本文件中
/// <summary>
/// exprot the data to txt
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrint_Click(object sender, EventArgs e)
{
string strLine = "";
if (sgItemErrorList.Rows.Count < 1)
{
AppInfo.ErrorMsg(this, "There Is No Record To Print");
}
else
{
this.Cursor = Cursors.WaitCursor;
string filename = "C:\\CST.txt";
FileStream sr = File.Open(filename, FileMode.Create);
for (int i = 0; i < sgItemErrorList.Rows.Count; i++)
{
strLine +=" "+sgItemErrorList.Rows[i].Cells[0].Value.ToString().PadRight(19,' ')
+ sgItemErrorList.Rows[i].Cells[1].Value.ToString().PadRight(34,' ')
+ sgItemErrorList.Rows[i].Cells[2].Value.ToString().PadRight(6,' ')
+ sgItemErrorList.Rows[i].Cells[3].Value.ToString().PadRight(9,' ')
+ sgItemErrorList.Rows[i].Cells[4].Value.ToString().PadRight(19,' ');
sw.WriteLine(strLine);
strLine = "";
}
sw.WriteLine("");
sw.WriteLine(" *** End of report *** ");
sw.Close();
sr.Close();
System.Diagnostics.Process.Start("notepad.exe", "c:\\CST.txt");
this.Cursor = Cursors.Default;
}
}
评论这张
转发至微博
转发至微博
评论