111,129
社区成员
发帖
与我相关
我的任务
分享
public static string exprotExcel(string title, DataGridView exprotGrid)
{
string message = "";
if (exprotGrid.Rows.Count == 0)
{
message = "当前数据为空,不执行导出";
return message;
}
Microsoft.Office.Interop.Excel.Application exlapp = new Microsoft.Office.Interop.Excel.Application();
if (exlapp == null)
{
message = "excel无法启动";
return message;
}
Microsoft.Office.Interop.Excel.Workbook exlbook = exlapp.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet exlsheet = (Microsoft.Office.Interop.Excel.Worksheet)exlbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range = exlsheet.get_Range(exlapp.Cells[1, 1], exlapp.Cells[1, exprotGrid.ColumnCount]);
int colIndex = 0;
int rowIndex = 0;
int colCount = exprotGrid.ColumnCount;
int rowCount = exprotGrid.RowCount;
object[,] objdata = new object[rowCount + 1, colCount];
for (colIndex = 0; colIndex < colCount; colIndex++)
{
objdata[rowIndex, colIndex] = exprotGrid.Columns[colIndex].HeaderText;
}
for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
if (exprotGrid[colIndex, rowIndex].Value != null)
{
objdata[rowIndex + 1, colIndex] = exprotGrid[colIndex, rowIndex].Value.ToString();
}
else
{
objdata[rowIndex + 1, colIndex] = "";
}
}
Application.DoEvents();
}
range = exlsheet.get_Range(exlapp.Cells[2, 1], exlapp.Cells[rowCount + 2, colCount]);
range.Value2 = objdata;
try
{
exlapp.Cells.EntireColumn.AutoFit();
exlapp.Visible = true;
}
catch
{
message = "保存出错,请检查";
return message;
}
return message;
}
/// <summary>
/// 把DataGridView中的数据 导出到EXCEL /// </summary>
/// <param name="dg"></param>
public static void input_excel(DataGridView dg)
{
Excel._Worksheet Sht;
Excel._Workbook Bo;
Excel.Application excel = new Excel.Application();
Bo = excel.Application.Workbooks.Add(true);
// excel.Visible = true;//excel是否显示
Sht = (Excel.Worksheet)Bo.Sheets[1];
//写入数据到EXCEL
int Rowed = 0;
if (dg.AllowUserToAddRows == true)
{
for (int i = 0; i < dg.Rows.Count - 1; i++)
{
for (int y = 1; y <= dg.ColumnCount; y++)
{
excel.Cells[1, y] = dg.Columns[y - 1].HeaderText;
}
Rowed++;
if (Rowed < 65000)
{
for (int lie = 0; lie < dg.ColumnCount; lie++)
{
excel.Cells[Rowed + 1, lie + 1] = Convert.ToString(dg[lie, i ].Value);
}
}
else
{
Sht = (Excel.Worksheet)Bo.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Rowed = 0;
i--;
}
}
}
else
{
for (int i = 0; i < dg.Rows.Count - 1; i++)
{
for (int y = 1; y <= dg.ColumnCount; y++)
{
excel.Cells[1, y] = dg.Columns[y - 1].HeaderText;
}
Rowed++;
if (Rowed < 65000)
{
for (int lie = 0; lie < dg.ColumnCount; lie++)
{
excel.Cells[Rowed + 1, lie + 1] = Convert.ToString(dg[lie, i].Value);
}
}
else
{
Sht = (Excel.Worksheet)Bo.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Rowed = 0;
i--;
}
}
}
excel.Visible = true;
}
/// <summary>
/// 把DataGridView中的数据 导出到EXCEL /// </summary>
/// <param name="dg"></param>
public static void input_excel(DataGridView dg)
{
Excel._Worksheet Sht;
Excel._Workbook Bo;
Excel.Application excel = new Excel.Application();
Bo = excel.Application.Workbooks.Add(true);
// excel.Visible = true;
Sht = (Excel.Worksheet)Bo.Sheets[1];
//写入数据到EXCEL
int Rowed = 0;
if (dg.AllowUserToAddRows == true)
{
LB.ListB("共计 " +Convert.ToString ( dg.Rows.Count-1) + "条", Lb);
LB.ListB("", Lb);
for (int i = 0; i < dg.Rows.Count - 1; i++)
{
for (int y = 1; y <= dg.ColumnCount; y++)
{
excel.Cells[1, y] = dg.Columns[y - 1].HeaderText;
}
Rowed++;
if (Rowed < 65000)
{
for (int lie = 0; lie < dg.ColumnCount; lie++)
{
excel.Cells[Rowed + 1, lie + 1] = Convert.ToString(dg[lie, i ].Value);
}
}
else
{
Sht = (Excel.Worksheet)Bo.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Rowed = 0;
i--;
}
}
}
else
{
LB.ListB("共计 " + Convert.ToString(dg.Rows.Count ) + "条", Lb);
LB.ListB("", Lb);
for (int i = 0; i < dg.Rows.Count - 1; i++)
{
for (int y = 1; y <= dg.ColumnCount; y++)
{
excel.Cells[1, y] = dg.Columns[y - 1].HeaderText;
}
Rowed++;
if (Rowed < 65000)
{
for (int lie = 0; lie < dg.ColumnCount; lie++)
{
excel.Cells[Rowed + 1, lie + 1] = Convert.ToString(dg[lie, i].Value);
}
}
else
{
Sht = (Excel.Worksheet)Bo.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Rowed = 0;
i--;
}
}
}
excel.Visible = true;
}
public static void allTable_toExcel(string outputFileName, System.Data.DataSet receivedDataSet, string receivedDataSetOfTableName)
{
try
{
if (receivedDataSet == null) return;
//string currentDate = DateTime.Now.ToShortDateString();
string currentDate = DateTime.Now.ToString();
string saveFileName = "";
currentDate = formatString(currentDate);
bool fileSaved = false;
System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
//saveDialog.DefaultExt = "xls";
saveDialog.DefaultExt = "csv";
//saveDialog.Filter = "Excel 文件|*.xls";
saveDialog.Filter = "csv 文件|*.csv|Excel 文件|*.xls";
saveDialog.FileName = outputFileName + "_" + currentDate;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
System.Windows.Forms.MessageBox.Show("无法创建Excel对象,可能您未安装Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
//写入字段
for (int i = 0; i < receivedDataSet.Tables[receivedDataSetOfTableName].Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = receivedDataSet.Tables[receivedDataSetOfTableName].Columns[i].ColumnName;
}
//写入数值
for (int r = 0; r < receivedDataSet.Tables[receivedDataSetOfTableName].Rows.Count; r++)
{
for (int i = 0; i < receivedDataSet.Tables[receivedDataSetOfTableName].Columns.Count; i++)
{
worksheet.Cells[r + 2, i + 1] = receivedDataSet.Tables[receivedDataSetOfTableName].Rows[r][i];
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。
//if (this.Text != "Notification")
{
Microsoft.Office.Interop.Excel.Range rg = worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[receivedDataSet.Tables[0].Rows.Count + 1, 2]);
rg.NumberFormat = "00000000";
}
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false;
System.Windows.Forms.MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
else
{
fileSaved = false;
}
xlApp.Quit();
GC.Collect();// 强行销毁
if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEl
}
catch (Exception exc)
{
exc.ToString();
}
}