如何解决将数据表导出到Excel时出现问题?
| 在这里,我将数据表导出到excel ..但是我遇到了类似“ 0”的错误,我已对该文件夹授予了完全权限。.我不明白这是什么问题。public void CreateCSVFile(System.Data.DataTable dt,string strFilePath)
{
StreamWriter sw = new StreamWriter(strFilePath,false);
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(\",\");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(\",\");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
有什么建议吗?
编辑:
private void button1_Click(object sender,EventArgs e)
{
DirectoryInfo Folder = new DirectoryInfo(textBox3.Text);
var strFilePath = Path.Combine(textBox3.Text.Trim(),\"Excel\");
if (Folder.Exists)
if (!Directory.Exists(strFilePath))
Directory.CreateDirectory(strFilePath);
string strConn = \"user id=**;password=***;initial catalog=***;data source=***;\";
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
System.Data.DataTable dt = new System.Data.DataTable();
SqlDataAdapter da = new SqlDataAdapter(\"select * from \" + textBox1.Text + \" where pomas_pono =\'\" + textBox2.Text+\"\'\",conn);
da.Fill(dt);
CreateCSVFile(dt,strFilePath);
conn.Close();
}
解决方法
尝试以下代码:
private void BindEntryCancellationAudit()
{
try
{
invoice = new BLL_Invoice();
DataTable dtAuditData = invoice.getAuditDetailsForInvoice();
progressBar1.Visible = true;
progressBar1.Minimum = 1;
progressBar1.Maximum = dtAuditData.Rows.Count;
progressBar1.Show();
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets[\"Sheet1\"];
worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.ActiveSheet;
worksheet.Name = \"Invoice Audit Details\";
for (int i = 1; i < dtAuditData.Columns.Count + 1; i++)
{
worksheet.Cells[1,i] = dtAuditData.Columns[i-1].ColumnName;
}
for (int i = 0; i < dtAuditData.Rows.Count - 1; i++)
{
for (int j = 0; j < dtAuditData.Columns.Count; j++)
{
worksheet.Cells[i + 2,j + 1] = dtAuditData.Rows[i][j].ToString();
worksheet.Cells.EntireColumn.AutoFit();
if (j + 1 < 17 && j + 1 > 12)
{
Excel.Range cell = (Excel.Range)worksheet.Cells[i + 2,j + 1];
cell.NumberFormat = \"0.0000\";
}
}
progressBar1.PerformStep();
}
string filepath = ConfigurationManager.AppSettings[\"ReportLocation\"].ToString();
workbook.SaveAs(filepath+\"\\\\AuditReport.xls\",Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,Type.Missing);
app.Quit();
progressBar1.Hide();
progressBar1.Visible = false;
System.Diagnostics.Process.Start(filepath+\"\\\\AuditReport.xls\");
progressBar1.Maximum = 0;
progressBar1.Minimum = 0;
}
catch (Exception ex)
{ throw ex; }
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。