我当前正在使用Microsoft.Office.Core;并使用Excel(Microsoft.Office.Interop.Excel)将我的数据从Windows表单导出到Excel。
导出文件时,它总是会被覆盖。听说我们可以使用“打开文件”对话框将文件保存在所需的位置。
我还包括了将数据导出到excel的方式
有人知道解决方法吗?
chart1.SaveImage("C:\\New folder\\mychart.png", ChartImageFormat.Png);
chart2.SaveImage("C:\\New folder\\mychart2.png", ChartImageFormat.Png);
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("Excel is not properly installed!!");
return;
}
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "Date and time Exported :";
xlWorkSheet.Cells[1, 2] = dateandtimelbl.Text;
xlWorkSheet.Cells[1, 3] = "Exported By :";
xlWorkSheet.Cells[1, 4] = lblemployeeno.Text;
xlWorkSheet.Cells[2, 1] = "PartNumber:";
xlWorkSheet.Cells[2, 2] = partnumbercomboBox.Text;
xlWorkSheet.Cells[2, 3] = "# Materials Type:";
xlWorkSheet.Cells[2, 4] = label15.Text;
xlWorkSheet.Cells[3, 1] = "Part Name:";
xlWorkSheet.Cells[3, 2] = label14.Text;
xlWorkSheet.Cells[2, 5] = "Characterstic:";
xlWorkSheet.Cells[2, 6] = label16.Text;
xlWorkSheet.Cells[3, 3] = "Specification:";
xlWorkSheet.Cells[3, 4] = label17.Text;
xlWorkSheet.Cells[2, 7] = "Operation:";
xlWorkSheet.Cells[2, 8] = label18.Text;
xlWorkSheet.Cells[3, 7] = "Period";
xlWorkSheet.Cells[3, 8] = label19.Text;
xlWorkSheet.Cells[2, 9] = "x̅";
xlWorkSheet.Cells[2, 10] = "UCLX";
xlWorkSheet.Cells[2, 11] = "LCLX";
xlWorkSheet.Cells[2, 12] = "Ṝ";
xlWorkSheet.Cells[2, 13] = "UCLR";
xlWorkSheet.Cells[2, 14] = "LCLR";
xlWorkSheet.Cells[3, 9] = label20.Text;
xlWorkSheet.Cells[3, 10] = label21.Text;
xlWorkSheet.Cells[3, 11] = label22.Text;
xlWorkSheet.Cells[3, 12] = label23.Text;
xlWorkSheet.Cells[3, 13] = label24.Text;
xlWorkSheet.Cells[3, 14] = label25.Text;
xlWorkSheet.Cells[64, 2] = "1";
xlWorkSheet.Cells[64, 3] = "2";
xlWorkSheet.Cells[64, 4] = "3";
xlWorkSheet.Cells[64, 5] = "4";
xlWorkSheet.Cells[64, 6] = "5";
xlWorkSheet.Cells[64, 7] = "6";
xlWorkSheet.Cells[64, 8] = "7";
xlWorkSheet.Cells[64, 9] = "8";
xlWorkSheet.Cells[64, 10] = "9";
xlWorkSheet.Cells[64, 11] = "10";
xlWorkSheet.Cells[64, 12] = "11";
xlWorkSheet.Cells[64, 13] = "12";
xlWorkSheet.Cells[64, 14] = "13";
xlWorkSheet.Cells[64, 15] = "14";
xlWorkSheet.Cells[64, 16] = "15";
xlWorkSheet.Cells[64, 17] = "16";
xlWorkSheet.Cells[64, 18] = "17";
xlWorkSheet.Cells[64, 19] = "18";
xlWorkSheet.Cells[64, 20] = "19";
xlWorkSheet.Cells[64, 21] = "20";
xlWorkSheet.Cells[64, 22] = "21";
xlWorkSheet.Cells[64, 23] = "22";
xlWorkSheet.Cells[64, 24] = "23";
xlWorkSheet.Cells[64, 25] = "24";
xlWorkSheet.Cells[64, 26] = "25";
xlWorkSheet.Cells[64, 27] = "26";
xlWorkSheet.Cells[64, 28] = "27";
xlWorkSheet.Cells[64, 29] = "28";
xlWorkSheet.Cells[64, 30] = "29";
xlWorkSheet.Cells[64, 31] = "30";
xlWorkSheet.Cells[64, 32] = "31";
xlWorkSheet.Cells[65, 1] = "Date";
xlWorkSheet.Cells[66, 1] = "Day(D)/Night(N)";
xlWorkSheet.Cells[67, 1] = "Top(1)";
xlWorkSheet.Cells[68, 1] = "Top(2)";
xlWorkSheet.Cells[69, 1] = "Bottom(1)";
xlWorkSheet.Cells[70, 1] = "Bottom(2)";
xlWorkSheet.Cells[71, 1] = "X-TOP";
xlWorkSheet.Cells[72, 1] = "X-BOT";
xlWorkSheet.Cells[73, 1] = "X-T/B";
xlWorkSheet.Cells[74, 1] = "Range";
xlWorkSheet.Cells[75, 1] = "Employee no";
xlWorkSheet.Cells[76, 1] = "Date and time";
xlWorkSheet.Shapes.AddPicture(@"C:\\New folder\\mychart.png", MsoTriState.msoFalse, MsoTriState.msoCTrue, 50, 70, 3600, 400);
xlWorkSheet.Shapes.AddPicture(@"C:\\New folder\\mychart2.png", MsoTriState.msoFalse, MsoTriState.msoCTrue, 50, 500, 3600, 400);
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
xlWorkSheet.Cells[i + 65, j + 2] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
xlWorkSheet.UsedRange.Rows.AutoFit();
xlWorkSheet.UsedRange.Columns.AutoFit();
xlWorkBook.SaveAs("d:\\csharp-Excel.xls",
Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue,
misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue,
misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
MessageBox.Show(
"Excel file created, you can find the file D:\\csharp-Excel.xls");
有没有办法做类似的事情:
//savefile
string saveFileName = "CalendarRubberReport";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "ExcelFile|*.xls";
saveDialog.FileName = saveFileName;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
if (saveDialog.ShowDialog() == DialogResult.OK)
{
try
{
saveFileName = saveDialog.FileName;
workbook.Write(ms);
System.IO.FileStream file = new System.IO.FileStream(saveFileName,
System.IO.FileMode.Create);
workbook.Write(file);
file.Close();
workbook = null;
ms.Close();
ms.Dispose();
MessageBox.Show(saveFileName + " Save Complete", "Complete",
MessageBoxButtons.OK);
} catch {
MessageBox.Show("Error file is open/in use");
}
} Else {
workbook = null;
ms.Close();
ms.Dispose();
}
}