使用C#设置Excel单元格值

时间:2018-07-06 14:48:14

标签: c# excel-interop

我使用以下代码设置Excel文件的单元格值:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

@ManagedBean
public class FileUploadView {

private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
}

public void upload() throws IOException {
    if (file != null) {
        FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);

        System.out.println("Uploaded file now: " + file.getFileName());

        String name = file.getFileName();

        InputStream inputStream = file.getInputstream();
        OutputStream outputStream = null;
        String path = "/Users/daryl/Desktop/";

        File file = new File(path + name);

        outputStream = new FileOutputStream(file);

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

        System.out.println("Done!");

    }
}
}

但是我一直得到

  

错误:HRESULT异常:0x800A03EC

为什么会出现此错误以及如何解决?

5 个答案:

答案 0 :(得分:0)

更新:

  try
            {

                saveFileDialog1.Title = "Save as Excel File";
                saveFileDialog1.FileName = "";
                saveFileDialog1.Filter = "Excel Files(2003)|*.xls|Excel Files(2007)|*.xls";
                if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                {
                    Microsoft.Office.Interop.Excel.Application worksheet = new Microsoft.Office.Interop.Excel.Application();
                    worksheet.Application.Workbooks.Add(Type.Missing);
                    worksheet.Columns.ColumnWidth = 20;
                    for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
                    {
                        worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;

                    }
                    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                    {
                        for (int j = 0; j < dataGridView1.Columns.Count; j++)
                        {
                            worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                        }

                    }
                    worksheet.ActiveWorkbook.SaveCopyAs(saveFileDialog1.FileName.ToString());
                    worksheet.ActiveWorkbook.Saved = true;
                    worksheet.Quit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

答案 1 :(得分:0)

我通常使用Value2属性(而不是Value

因此,您的代码如下:

for (int i = 2; i < rowCount; i++)
{
    for (int j = 1; j < columnCount; j++)
    {
        worksheet.Cells[i, j].Value2 = enrollmentDataGrid.Columns[j].GetCellContent(enrollmentDataGrid.Items[i]);
    }
 }

答案 2 :(得分:0)

https://www.codeproject.com/Questions/470089/Exception-from-HRESULT-x-A-EC-Error

  

您似乎并不是唯一的一个人:   http://stackoverflow.com/questions/7099770/hresult-0x800a03ec-on-worksheet-range[ ^]。

     

根据帖子,当您使用   Excel 2007或2010中打开的旧(xls)工作簿。如果是这种情况,   访问之前,请先尝试将文件另存为新格式的工作簿   细胞。

答案 3 :(得分:0)

尝试此代码

for (int i = 1; i < columnCount + 1; i++)
                    {
                        worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;

                    }



 for (int i = 0; i < rowCount -1; i++)
    {
        for (int j = 0; j < columnCount; j++)
        {
            worksheet.Cells[i+2, j+1].Value2 = enrollmentDataGrid.Columns[j].GetCellContent(enrollmentDataGrid.Items[i]);
        }
     }

答案 4 :(得分:0)

当我在C#中使用wpf框架编写导出到xls时。我也遇到了这个问题,并使用以下代码解决了这个问题。尝试从此代码中查看

 Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            Microsoft.Office.Interop.Excel._Workbook workb = app.Workbooks.Open("c:\\Users\\Jack\\Documents\\Document1.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
            worksheet = workb.Sheets[1];
           // worksheet = workbook.Sheets[1];
            //worksheet = workbook.ActiveSheet;
            //worksheet.Name = "birinchi";




            for (int i = 0; i < Table.Columns.Count; i++) 
            {
             //   worksheet.Cells[1, i+1] = Table.Columns[i].Header;

            }


                for (int i = 0; i < Table.Columns.Count; i++)
                {

                    for (int j = 0; j < Table.Items.Count; j++)
                    {
                        TextBlock b = Table.Columns[i].GetCellContent(Table.Items[j]) as TextBlock;
                        worksheet.Cells[j + 2, i + 1] = b.Text;
                        //MessageBox.Show(b.Text);

                    }
                }

                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.FileName = "Document";
                dlg.DefaultExt = ".xlsx";
                Nullable<bool> result = dlg.ShowDialog();
                if (result == true)
                {

                   workbook.SaveAs(dlg.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);



                }
                app.Quit();