如何在c#中保存工作簿 [英] How save workbook in c#
本文介绍了如何在c#中保存工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在保存工作簿
workbook.save();
但它显示的是saveas对话框。
i需要保存工作簿而不显示saveaas dialouge框。
I am saving workbook
workbook.save();
but it is showing saveas dialouge box.
i need to save workbook without showing saveaas dialouge box.
推荐答案
excel = new Excel.ApplicationClass();
workbooks = excel.Workbooks;
workbook = workbooks.Add(true);
ExportCurrentData(ref excel,dsData, dataSetIndex);
worksheet = (Excel.Worksheet)excel.ActiveSheet;
worksheet.Activate();
excel.saveworkspace();
private object m_strSampleFolder = "C:\\Projects\\ExcelProject";
private void btnConvertToExcel_Click(object sender, EventArgs e)
{
Excel._Workbook objBook;
Excel.Workbooks objBooks;
Excel.Sheets objSheets;
Excel._Worksheet objSheet;
Excel.Range range;
try
{
Excel.Application objApp;
// Instantiate Excel and start a new workbook.
objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add(Missing.Value);
objSheets = objBook.Worksheets;
objSheet = (Excel._Worksheet)objSheets.get_Item(1);
range = objSheet.get_Range("A1", Missing.Value);
range = range.get_Resize(c1FlexGrid1.Rows.Count, c1FlexGrid1.Cols.Count);
string sql = "SELECT * FROM AccessHistory";
SqlDataAdapter DA = new SqlDataAdapter(sql, Connstr);
DataSet ds = new DataSet();
DA.Fill(ds);
//Create an array.
string[,] tempArray = new string[c1FlexGrid1.Rows.Count, c1FlexGrid1.Cols.Count];
//Fill the array.
for (int RRow = 0; RRow < c1FlexGrid1.Rows.Count - 1; RRow++)
{
for (int RColm = 0; RColm < c1FlexGrid1.Cols.Count - 1; RColm++)
{
tempArray[RRow, RColm] = ds.Tables[0].Rows[RRow].ItemArray[RColm].ToString();
}
}
//Set the range value to the array.
range.set_Value(System.Reflection.Missing.Value, tempArray);
//Return control of Excel to the user.
//objApp.Visible = true;
//objApp.UserControl = true;
object misValue = System.Reflection.Missing.Value;
objBook.SaveAs(m_strSampleFolder + "AccessHistory.xlsx", Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
objBook.Close(true, misValue, misValue);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
MessageBox.Show("NOw in the finally block");
Process objApp=Process.Start(m_strSampleFolder + "AccessHistory.xlsx");
}
}
保存前请添加以下行。
Just add below line before saving.
excelApp.DisplayAlerts = false;
这篇关于如何在c#中保存工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文