快速将DataGridView中的数据导入EXCEL:
private void button3_Click(object sender, EventArgs e)
{
if (this.datagridview1.RowCount < 1)
{
MessageBox.Show("没有可以导出的数据!");
return;
} Microsoft.Office.Interop.Excel.Application appExcel; appExcel = new Microsoft.Office.Interop.Excel.Application();
Workbook workbookData;
Worksheet worksheetData;
Range rangedata; appExcel.Visible = true; // set culture to US
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
workbookData = appExcel.Workbooks.Add(Missing.Value);
worksheetData = (Worksheet)workbookData.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
worksheetData.Name = "Saved";
for (int i = 0; i < this.datagridview1.Columns.Count; i++)
{
worksheetData.Cells[1, i + 1] = this.datagridview1.Columns[i].HeaderText;
} rangedata = worksheetData.get_Range("A2", Missing.Value); Range xlRang = null;
int iRowCount = this.datagridview1.RowCount - 1;
int iParstedRow = 0, iCurrSize = 0;
int iEachSize = 1000; // each time you
int iColumnAccount = this.datagridview1.Columns.Count;
object[,] objVal = new object[iEachSize, iColumnAccount];
try
{
iCurrSize = iEachSize;
while (iParstedRow < iRowCount)
{
if ((iRowCount - iParstedRow) < iEachSize)
iCurrSize = iRowCount - iParstedRow;
for (int i = 0; i < iCurrSize; i++)
{
for (int j = 0; j < iColumnAccount; j++)
objVal[i, j] = this.datagridview1.Rows[i].Cells[j].Value.ToString();
}
// Get Save Range from Excel WorkSheet
// such as A1 H10, means From A to H Columns, and 1 to 10 rows
xlRang = worksheetData.get_Range("A" + ((int)(iParstedRow + 2)).ToString(), ((char)('A' + iColumnAccount - 1)).ToString() + ((int)(iParstedRow + iCurrSize + 1)).ToString()); xlRang.Value2 = objVal; iParstedRow = iParstedRow + iCurrSize;
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRang);
xlRang = null; }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
} // return to previous culture
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI; }
private void button3_Click(object sender, EventArgs e)
{
if (this.datagridview1.RowCount < 1)
{
MessageBox.Show("没有可以导出的数据!");
return;
} Microsoft.Office.Interop.Excel.Application appExcel; appExcel = new Microsoft.Office.Interop.Excel.Application();
Workbook workbookData;
Worksheet worksheetData;
Range rangedata; appExcel.Visible = true; // set culture to US
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
workbookData = appExcel.Workbooks.Add(Missing.Value);
worksheetData = (Worksheet)workbookData.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
worksheetData.Name = "Saved";
for (int i = 0; i < this.datagridview1.Columns.Count; i++)
{
worksheetData.Cells[1, i + 1] = this.datagridview1.Columns[i].HeaderText;
} rangedata = worksheetData.get_Range("A2", Missing.Value); Range xlRang = null;
int iRowCount = this.datagridview1.RowCount - 1;
int iParstedRow = 0, iCurrSize = 0;
int iEachSize = 1000; // each time you
int iColumnAccount = this.datagridview1.Columns.Count;
object[,] objVal = new object[iEachSize, iColumnAccount];
try
{
iCurrSize = iEachSize;
while (iParstedRow < iRowCount)
{
if ((iRowCount - iParstedRow) < iEachSize)
iCurrSize = iRowCount - iParstedRow;
for (int i = 0; i < iCurrSize; i++)
{
for (int j = 0; j < iColumnAccount; j++)
objVal[i, j] = this.datagridview1.Rows[i].Cells[j].Value.ToString();
}
// Get Save Range from Excel WorkSheet
// such as A1 H10, means From A to H Columns, and 1 to 10 rows
xlRang = worksheetData.get_Range("A" + ((int)(iParstedRow + 2)).ToString(), ((char)('A' + iColumnAccount - 1)).ToString() + ((int)(iParstedRow + iCurrSize + 1)).ToString()); xlRang.Value2 = objVal; iParstedRow = iParstedRow + iCurrSize;
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRang);
xlRang = null; }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
} // return to previous culture
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI; }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货