0

这就是我生成 Excel 变量的方式:

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelworkBook;
Microsoft.Office.Interop.Excel.Worksheet excelSheet;
Microsoft.Office.Interop.Excel.Range excelCellrange;
excelworkBook = excel.Application.Workbooks.Add(Type.Missing);
excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelworkBook.ActiveSheet;
//proper data from xml data field to store in excel
//        ************************
//generate a data table and fill excell cell 
 excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], 
 excelSheet.Cells[theDataSet.Tables[0].Rows.Count+1, theDataSet.Tables[0].Columns.Count]];
 excelCellrange.Font.ThemeColor = 
 Microsoft.Office.Interop.Excel.XlThemeColor.xlThemeColorAccent1;
 excelCellrange.EntireColumn.AutoFit();
 Microsoft.Office.Interop.Excel.Borders border = excelCellrange.Borders;
 border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
 border.Weight = 3d;
 excel.Columns.AutoFit();
 excel.DisplayAlerts = false;
 excel.Visible = true;

现在的问题是如何将这个 excel 变量保存到真正的 excel 文件到特定的目的地?此致。

4

1 回答 1

0

这就是我如何找出可能对其他人有帮助的问题:

Microsoft.Office.Interop.Excel.Application excel = new 
Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelworkBook;
Microsoft.Office.Interop.Excel.Worksheet excelSheet;
Microsoft.Office.Interop.Excel.Range excelCellrange;
excelworkBook = excel.Application.Workbooks.Add(Type.Missing);
excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelworkBook.ActiveSheet;
XmlDocument xml = new XmlDocument();
xml.LoadXml(dtAll.Rows[gridEX1.CurrentRow.RowIndex]["Data"].ToString());
StringReader theReader = new StringReader(xml.InnerXml);
DataSet theDataSet = new DataSet();
theDataSet.ReadXml(theReader);
excel.Caption = dtAll.Rows[gridEX1.CurrentRow.RowIndex]["docID"].ToString() + "_" + dtAll.Rows[gridEX1.CurrentRow.RowIndex]["DocTitle"].ToString() + "_" + dtAll.Rows[gridEX1.CurrentRow.RowIndex]["DocDetails"].ToString();
excel.StandardFont = "B Nazanin";
excel.StatusBar = "col count: " + theDataSet.Tables[0].Rows.Count;
for (int i = 0; i < theDataSet.Tables[0].Columns.Count; i++)
{
   excel.Cells[1, i + 1] = theDataSet.Tables[0].Columns[i].ColumnName;
   excel.Cells[1, i + 1].Font.Color = System.Drawing.Color.Blue;
   for (int j = 0; j < theDataSet.Tables[0].Rows.Count; j++)
     {
         excel.Cells[j + 2, i + 1].Font.Color = System.Drawing.Color.Black;
         excel.Cells[j + 2, i + 1] = theDataSet.Tables[0].Rows[j][i].ToString();
     }
}
excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[theDataSet.Tables[0].Rows.Count + 1, theDataSet.Tables[0].Columns.Count]];
excelCellrange.Font.ThemeColor = Microsoft.Office.Interop.Excel.XlThemeColor.xlThemeColorAccent1;
excelCellrange.EntireColumn.AutoFit();
Microsoft.Office.Interop.Excel.Borders border = excelCellrange.Borders;
border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border.Weight = 3d;
                    
byte[] output = Encoding.UTF8.GetBytes(xml.InnerXml);
string strfn = GroupTitle + ".xlsx";
if (File.Exists(strfn))
  {
      File.Delete(strfn);
  } 
FileStream fs = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
fs.Write(output, 0, output.Length);
fs.Flush();
fs.Close();

string dest = Global.DocOutputPath.Substring(0, Global.DocOutputPath.Length - 1) + GroupTitle + ".xlsx";
if (File.Exists(dest))
{
    DialogResult dialogResult = MessageBox.Show("are you sure about rewrite on file?", "warnning", MessageBoxButtons.YesNo);
    if (dialogResult == DialogResult.Yes)
     {
        File.Copy(strfn, dest);
        File.Delete(dest);
     }
    else if (dialogResult == DialogResult.No)
     {
         return;
     }
 }
 else
 {
     File.Copy(strfn, dest);
 }
于 2021-08-01T10:13:00.797 回答