public static bool ExportForDataGridview(DataGridView DataGrid1, string fileName, bool isShowExcle)
{ #region 导出数据到Excel表
//建立Excel对象
Excel.Application app = new Excel.Application();
try
{
if (app == null)
{
MessageBox.Show("对不起,您的电脑未安装Excel,请先安装后再进行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
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)
{
return false;
}
string sLen = "";
//取得最后一列列名
char H = (char)(64 + DataGrid1.ColumnCount / 26);
char L = (char)(64 + DataGrid1.ColumnCount % 26);
if (DataGrid1.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[DataGrid1.ColumnCount];
for (int i = 0; i < DataGrid1.ColumnCount; i++)
{
asCaption[i] = DataGrid1.Columns[i].HeaderText;
}
ranCaption.Value2 = asCaption; //数据
object[] obj = new object[DataGrid1.Columns.Count];
for (int r = 0; r < DataGrid1.RowCount - 1; r++)
{
for (int l = 0; l < DataGrid1.Columns.Count; l++)
{
if (DataGrid1[l, r].ValueType == typeof(DateTime))
{
obj[l] = DataGrid1[l, r].Value.ToString();
}
else
{
obj[l] = DataGrid1[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;
}
MessageBox.Show("恭喜,导出Excel表成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//保存
workbook.SaveCopyAs(fileName);
workbook.Saved = true; }
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
MessageBox.Show("对不起,导出Excel表失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
//关闭
app.UserControl = false;
app.Quit();
}
return true;
#endregion
}
private void btnOut_Click(object sender, EventArgs e)
{
//导出数据
if (MessageBox.Show("数据导出功能需要专业人员技术支持,请慎重操作!您确定要将Excel表里面的数据导入到数据库里面么?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{ ExportForDataGridview(DataGrid1,"路径名称", false); }
}
{ #region 导出数据到Excel表
//建立Excel对象
Excel.Application app = new Excel.Application();
try
{
if (app == null)
{
MessageBox.Show("对不起,您的电脑未安装Excel,请先安装后再进行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
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)
{
return false;
}
string sLen = "";
//取得最后一列列名
char H = (char)(64 + DataGrid1.ColumnCount / 26);
char L = (char)(64 + DataGrid1.ColumnCount % 26);
if (DataGrid1.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[DataGrid1.ColumnCount];
for (int i = 0; i < DataGrid1.ColumnCount; i++)
{
asCaption[i] = DataGrid1.Columns[i].HeaderText;
}
ranCaption.Value2 = asCaption; //数据
object[] obj = new object[DataGrid1.Columns.Count];
for (int r = 0; r < DataGrid1.RowCount - 1; r++)
{
for (int l = 0; l < DataGrid1.Columns.Count; l++)
{
if (DataGrid1[l, r].ValueType == typeof(DateTime))
{
obj[l] = DataGrid1[l, r].Value.ToString();
}
else
{
obj[l] = DataGrid1[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;
}
MessageBox.Show("恭喜,导出Excel表成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//保存
workbook.SaveCopyAs(fileName);
workbook.Saved = true; }
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
MessageBox.Show("对不起,导出Excel表失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
//关闭
app.UserControl = false;
app.Quit();
}
return true;
#endregion
}
private void btnOut_Click(object sender, EventArgs e)
{
//导出数据
if (MessageBox.Show("数据导出功能需要专业人员技术支持,请慎重操作!您确定要将Excel表里面的数据导入到数据库里面么?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{ ExportForDataGridview(DataGrid1,"路径名称", false); }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货